Thanks for the tip!

On Thursday, June 19, 2014 6:52:26 PM UTC+1, Matt Bauman wrote:
>
> I'd probably just do this:
>
> type FieldObj{R}
>    data::Array{Float64, R}
>    meta::Dict{ASCIIString, Union(Int64, Float64)}
>    N::Int64
>    rank::Int64
>    function FieldObj(data::Array{Float64, R}, meta::Dict{ASCIIString, 
> Union(Int64, Float64)}=default_meta())
>      new(data, meta, size(data, R), R-2)
>    end
> end
>
> typealias ScalarField FieldObj{2}
> typealias VectorField FieldObj{3}
>
> You don't need all those definitions!  You can combine your two outer 
> constructors using a default value for meta, and then if you're not going 
> to be using FieldObj itself as a constructor, there's no need for the outer 
> parametric constructor function, either.  
>
> julia> ScalarField(ones(100,100))
> FieldObj{2}(100x100 Array{Float64,2}: …
>
> julia> VectorField(ones(100,100))
> ERROR: no method FieldObj{3}(Array{Float64,2})
>
> Typically, I try not to define inner methods as the default ones are very 
> useful.  But in this case, it may be what you're after.
>
> On Thursday, June 19, 2014 1:14:40 PM UTC-4, Davide Lasagna wrote:
>>
>> HI,
>>
>> Coming from the Python world is a big step, especially when dealing with 
>> parametric types and their constructors. But Julia looks so promising and 
>> it is so fun to code with that i do not give up. I have an issue, though.
>>
>> Say I define the following parametric type:
>> type FieldObj{R}
>>     data::Array{Float64, R}
>>     meta::Dict{ASCIIString, Union(Int64, Float64)}
>>     N::Int64
>>     rank::Int64
>>     function FieldObj(data::Array{Float64, R}, meta::Dict{ASCIIString, 
>> Union(Int64, Float64)})
>>         new(data, meta, size(data, R), R-2)
>>     end
>> end
>>
>> function FieldObj{R}(data::Array{Float64, R}, meta::Dict{ASCIIString, 
>> Union(Int64, Float64)})
>>     FieldObj{R}(data, meta)
>> end
>>
>> function FieldObj{R}(data::Array{Float64, R})
>>     FieldObj{R}(data, default_meta())
>> end
>> where the parameter R specifies the dimension of the array data. (Is this 
>> possible at all??)
>>
>> I can now instantiate objects as
>> phi = FieldObj{2}(ones(100, 100))
>> ok. 
>>
>> However, i want to define some  typealiases, to have a nicer interface, 
>> and because operators will have a different meaning for each R. Say the 
>> typealiases are:
>> typealias ScalarField FieldObj{2}
>> typealias VectorField FieldObj{3}
>>
>> With these aliases I cannot directly create new objects and I get
>> julia> phi = ScalarField(ones(100, 100))
>> ERROR: no method FieldObj{2}(Array{Float64,2})
>>
>> However, if I define explicitly the constructors for the typealiases this 
>> is possible, say I define:
>>  function ScalarField(data::Array{Float64, 2})
>>     FieldObj{2}(data, default_meta())
>> end
>> which works.
>>
>> Now, I have two questions:
>> 1) Is there a way to avoid defining all the constructors explicitly by 
>> hand, say with an @eval block? 
>> 2) Why do I need to define these at all? What is "exactly" a typealias 
>> then?
>>
>> Thank you.
>>
>> Davide
>>
>>
>>
>>
>>
>>

Reply via email to