[julia-users] Re: How to correctly create custom representation of an array of a custom type?

2015-11-06 Thread Gnimuc Key
Another question:

Now I know that strange whitespace comes from array's indent:

julia> [Foo(1,2), Foo(1,2)]
2-element Array{Foo,1}:
 x ---> 1
y ---> 2

 x ---> 1
y ---> 2

How can I define my own `show(io::IO, x::Array{Foo, 1})`?

julia> function show(io::IO, x::Array{Foo, 1})
   println(io, "my Foo array")
   end
show (generic function with 99 methods)

julia> [Foo(1,2)]
1-element Array{Foo,1}:
 x ---> 1
y ---> 2

I tried this, but failed. 

A workaround is to overload `display`, are there any side effects of doing 
this?


[julia-users] Re: How to correctly create custom representation of an array of a custom type?

2015-11-06 Thread Gnimuc Key
that works, thank you!

在 2015年11月6日星期五 UTC+8下午11:23:55,Matt Bauman写道:
>
> Whenever you define an IO method, you need to make sure that all IO 
> methods you call within it also use that argument.  Change println("x ...") 
> to println(io, "x …").
>
> On Friday, November 6, 2015 at 8:17:56 AM UTC-5, Gnimuc Key wrote:
>>
>> we can define a good representation of a new type by overloading 
>> `Base.show(io::IO, x::Foo)`:
>>
>> ```
>>
>> import Base: show
>>
>>
>> type Foo
>> x
>> y
>> end
>>
>> function show(io::IO, x::Foo)
>> println("x ---> $(x.x)")
>> println("y ---> $(x.y)")
>> end
>>
>> julia> Foo(1,2)
>> x ---> 1
>> y ---> 2
>>
>> ``` 
>>
>> but I don't know why julia will show `Foo` 3 times when running 
>> `[Foo(1,2)]`:
>>
>> ```
>>
>> julia> [Foo(1,2)]
>> 1-element Array{Foo,1}:
>> x ---> 1
>> y ---> 2
>>  x ---> 1
>> y ---> 2
>> x ---> 1
>> y ---> 2
>>
>> ```
>>
>> BTW, the whitespace in the beginning of 3rd line is so wired...
>>  
>>
>

[julia-users] How to correctly create custom representation of an array of a custom type?

2015-11-06 Thread Gnimuc Key
we can define a good representation of a new type by overloading 
`Base.show(io::IO, x::Foo)`:

```

import Base: show


type Foo
x
y
end

function show(io::IO, x::Foo)
println("x ---> $(x.x)")
println("y ---> $(x.y)")
end

julia> Foo(1,2)
x ---> 1
y ---> 2

``` 

but I don't know why julia will show `Foo` 3 times when running 
`[Foo(1,2)]`:

```

julia> [Foo(1,2)]
1-element Array{Foo,1}:
x ---> 1
y ---> 2
 x ---> 1
y ---> 2
x ---> 1
y ---> 2

```

BTW, the whitespace in the beginning of 3rd line is so wired...
 


[julia-users] Re: Avoid type Unions in fields?

2015-10-28 Thread Gnimuc Key
that makes sense. many thanks!

在 2015年10月28日星期三 UTC+8下午3:14:26,Tomas Lycken写道:
>
> With a type declared like that, any access of the field x will be type 
> unstable, which means that Julia’s JIT compiler will emit much more 
> defensive, and thus slower, code.
>
> The solution is to declare a *parametric* type:
>
> type MyType{T<:Union{Int64, Float64}}
> x::T
> end
>
> That way, MyType(3) and MyType(3.0) will now be instances of different 
> types, and the compiler can generate fast code. (You can still write 
> functions that dispatch on just MyType, so you don’t loose any expressive 
> power…)
>
> // T
>
> On Wednesday, October 28, 2015 at 5:10:09 AM UTC+1, Gnimuc Key wrote:
>
> Avoid type Unions in fields ¶
>>>
>>> <http://docs.julialang.org/en/latest/manual/style-guide/#avoid-strange-type-unions>
>>> type MyType
>>> ...
>>> x::Union{Void,T}
>>> end
>>
>>  
>> I'm wondering whether it's a good style or not to write something like 
>> this:
>>
>> type MyType
>> ...
>> x::Union{Int64,Float64}end
>>
>> what's the side effects of using Union like this?
>>
> ​
>


[julia-users] Avoid type Unions in fields?

2015-10-27 Thread Gnimuc Key

>
> Avoid type Unions in fields ¶
>
> 
> type MyType
> ...
> x::Union{Void,T}
> end

 
I'm wondering whether it's a good style or not to write something like this:

type MyType
...
x::Union{Int64,Float64}end

what's the side effects of using Union like this?


Re: [julia-users] How to have type parameters T less than DenseArray{AbstractFloat} in the covariant sense?

2015-10-10 Thread Gnimuc Key

>
> type Foo{T<:AbstractFloat}
> x:: DenseArray{T}
> Foo() = new(call(T, 100))
> end


do you mean this?

在 2015年10月10日星期六 UTC+8下午9:36:39,cheng wang写道:
>
> I also want to use it to create a new type like the following (just 
> example, not correct code):
> type Foo{F<:AbstractFloat, T x::T{F}
> Foo() = new(call(T, F, 100))
> end
>
> The method you mentioned works for function, but it does not work for type.
>
> On Saturday, October 10, 2015 at 8:30:06 AM UTC+2, Tomas Lycken wrote:
>>
>> In the meantime, you can use 
>>
>> foo{T<:AbstractFloat}(x::DenseArray{T}) 
>>
>> to get what you want. 
>>
>> // T
>>
>>

Re: [julia-users] Re: what does Array{Type{T},1} mean?

2015-10-09 Thread Gnimuc Key

>
> (Don't be misled by the printout after the line, its the right hand side 
> of the assignment, not a)
>
yes, you're right, i was not aware of that. it seems julia will implicitly 
convert Array{DataType, 1} to Array{Type{T}, 1} without checking further 
type information.
i don't know whether this is the right behavior or a bug as @Yichao Yu 
mentioned.  i'm wondering what's the relationship between the type of an 
element of the array and the eltype of the array.

julia> a = Array{Int64, 1}(1)
1-element Array{Int64,1}:
 4635899120

julia> a[1] = 1.0
1.0

jjulia> typeof(a[1]) <: eltype(a)
true

`typeof(a[1]) <: eltype(a)` this rule is not true in the case of 
`Array{Type{T}}`, this is because there is no clear relationship between 
`DataType` and `Type{S}`, where S is a TypeVar with its `ub != Any`.  


在 2015年10月10日星期六 UTC+8上午6:03:42,ele...@gmail.com写道:
>
> The line:
>
>  a[:] = [Int64, Float32] 
>
> Sets the values of the elements of a[] but does not change its type.
>
> (Don't be misled by the printout after the line, its the right hand side 
> of the assignment, not a)
>
> Cheers
> Lex
>
> On Saturday, October 10, 2015 at 3:32:56 AM UTC+10, Yichao Yu wrote:
>>
>> On Fri, Oct 9, 2015 at 1:24 PM, Gnimuc Key  wrote: 
>> > so there are no difference between Array{DataType, 1} and 
>> Array{Type{Int64}, 
>> > 1} just as the example showed?   what about `foo(x::Array{DataType,1}) 
>>  = 
>> x` 
>> > and `foo(x::Array{Type{Int64}, 1})`? 
>>
>> I think this is a bug in codegen. The intrinsic itself looks fine 
>>
>> julia> Base.arrayset(Vector{Type{Int}}(2), Float32, 1) 
>> ERROR: TypeError: arrayset: expected Type{Int64}, got Type{Float32} 
>>
>>
>> > 
>> > 在 2015年10月10日星期六 UTC+8上午1:17:25,Cedric St-Jean写道: 
>> >> 
>> >> Array{Int64, 1} is an array of Ints. Type{Int64} is the type of a 
>> type, so 
>> >> Array{Type{Int64}} is an array of types. 
>> >> 
>> >> On Friday, October 9, 2015 at 1:11:25 PM UTC-4, Gnimuc Key wrote: 
>> >>> 
>> >>> i think `Array{Type{Int64},1}` is a vector whose element type is 
>> >>> constrained to `Int64`, but i'm confused by the outputs of the code 
>> below: 
>> >>> 
>> >>> 
>> >>> julia> a = Array{Type{Int64}}(2) 
>> >>> 2-element Array{Type{Int64},1}: 
>> >>>  #undef 
>> >>>  #undef 
>> >>> 
>> >>> julia> a[:] = [Int64, Float32] 
>> >>> 2-element Array{DataType,1}: 
>> >>>  Int64 
>> >>>  Float32 
>> >>> 
>> >>> julia> isa(a[2], eltype(a)) 
>> >>> false 
>> >>> 
>> >>> 
>> >>> julia> a 
>> >>> 2-element Array{Type{Int64},1}: 
>> >>>  Int64 
>> >>>  Float32 
>> >>> 
>> >>> 
>> >>> the last line is so wired. 
>>
>

[julia-users] Re: what does Array{Type{T},1} mean?

2015-10-09 Thread Gnimuc Key
so there are no difference between Array{DataType, 1} and 
Array{Type{Int64}, 1} just as the example showed?   what about 
`foo(x::Array{DataType,1})  = x` and `foo(x::Array{Type{Int64}, 1})`?  

在 2015年10月10日星期六 UTC+8上午1:17:25,Cedric St-Jean写道:
>
> Array{Int64, 1} is an array of Ints. Type{Int64} is the type of a type, so 
> Array{Type{Int64}} is an array of types.
>
> On Friday, October 9, 2015 at 1:11:25 PM UTC-4, Gnimuc Key wrote:
>>
>> i think `Array{Type{Int64},1}` is a vector whose element type 
>> is constrained to `Int64`, but i'm confused by the outputs of the code 
>> below:
>>
>>
>> julia> a = Array{Type{Int64}}(2)
>> 2-element Array{Type{Int64},1}:
>>  #undef
>>  #undef
>>
>> julia> a[:] = [Int64, Float32]
>> 2-element Array{DataType,1}:
>>  Int64  
>>  Float32
>>
>> julia> isa(a[2], eltype(a))
>> false
>>
>>
>> julia> a
>> 2-element Array{Type{Int64},1}:
>>  Int64  
>>  Float32
>>
>>
>> the last line is so wired. 
>>
>

[julia-users] what does Array{Type{T},1} mean?

2015-10-09 Thread Gnimuc Key
i think `Array{Type{Int64},1}` is a vector whose element type 
is constrained to `Int64`, but i'm confused by the outputs of the code 
below:


julia> a = Array{Type{Int64}}(2)
2-element Array{Type{Int64},1}:
 #undef
 #undef

julia> a[:] = [Int64, Float32]
2-element Array{DataType,1}:
 Int64  
 Float32

julia> isa(a[2], eltype(a))
false


julia> a
2-element Array{Type{Int64},1}:
 Int64  
 Float32


the last line is so wired. 


Re: [julia-users] Method parameters and union types

2015-07-27 Thread Gnimuc Key
if a = Union(T,U) where T is a Datatype, you can get it from a.types. 


在 2015年6月12日星期五 UTC+8上午3:53:43,David Gold写道:
>
> Just the opposite =p I don't know what T is, but I'd like to be able work 
> with it. For instance, if somebody gives me an Array{Union(T, U)}, where I 
> know U but not T, I'd like to be able to return an Array{T}. 
>
> On Thursday, June 11, 2015 at 3:29:02 PM UTC-4, Jameson wrote:
>>
>> Are you looking for the type-intersection perhaps?
>>
>> julia> typeintersect(Union(Int,Float64), Float64)
>> Float64
>>
>>
>> On Thu, Jun 11, 2015 at 3:15 PM David Gold  wrote:
>>
>>> I want the following function
>>>
>>> function t_or_void{T}(::Type{Union(T, Void)})
>>> return T
>>> end
>>>
>>> to work like this:
>>>
>>> julia> t_or_void(Union(Int, Void))
>>> Int64
>>>
>>> But in reality, it does this:
>>>
>>> julia> t_or_void(Union(Int, Void))
>>> UnionType
>>>
>>> Is there a way to make this work, or a way to extract T from Union(T, 
>>> Void)? The 'Void' isn't special -- it could be any determinate type.
>>>
>>> Thanks,
>>> D
>>>
>>