Along the way I discovered that overloading either call or convert does the 
job.



# some example
type nfoo{N, T}
a::NTuple{N, T}
end

# this works ok!
println(nfoo((1, 2, 3)))

# define alias
typealias twofoo{T} nfoo{2, T}

# need to specify parameter here
println(twofoo{Int}((1, 2)))

# this works
Base.call{T}(::Type{twofoo}, tup::NTuple{2, T}) = twofoo{T}(tup)

println(twofoo((1, 2)))

# this works too
Base.convert{T}(::Type{twofoo}, tup::NTuple{2, T}) = twofoo{T}(tup)

println(twofoo((1, 2)))


Which of the two is more appropriate? Is it that there are two ways of 
achieving the same thing?

Cheers

On Monday, December 7, 2015 at 11:57:37 PM UTC, Stefan Karpinski wrote:
>
> Yes, you can overload the call operator for arbitrary types.
>
> On Mon, Dec 7, 2015 at 6:44 PM, Davide Lasagna <[email protected] 
> <javascript:>> wrote:
>
>> Sorry to dig up this old post, but I wonder if this has been 
>> solved/addressed in 0.4?
>>
>> Thanks
>>
>>
>> On Friday, June 28, 2013 at 12:51:26 AM UTC+1, Kevin Squire wrote:
>>>
>>> Short non-answer: if declare something as a typealias with type 
>>> parameters, you can use it to match types, but (as you've found out) you 
>>> can't use it as a constructor.
>>>
>>> I asked a very related question recently, and the response was that 
>>> using them as constructors wouldn't be supported.  (I'm still hoping Jeff 
>>> Bezanson reconsiders, but perhaps it's technically challenging or 
>>> inefficient.)
>>>
>>> https://github.com/JuliaLang/julia/issues/3427
>>>
>>> If you don't care about matching types, you can just not declare it as a 
>>> typealias, and it will look and act like a constructor.
>>>
>>> In the issue above, I got around this limitation by the inelegant 
>>> solution of naming the typealias with an underscore, and creating the 
>>> function as you did above:
>>>
>>> typealias _OrderedDict{K,V} OrderedDictBase{K,V,LinkedDictItem,
>>> LinkedDictItem}
>>> OrderedDict(K::Type, V::Type) = OrderedDictBase{K,V,LinkedDictItem,
>>> LinkedDictItem}()
>>>
>>> Kevin
>>>
>>> On Thursday, June 27, 2013 2:28:00 PM UTC-7, [email protected] wrote:
>>>>
>>>> I have a question regarding the way constructors work in parametric 
>>>> typealias. Let me sketch using an example.
>>>> Suppose I started by defining a general pair type:
>>>> type Pair{T1,T2}
>>>>     element1::T1
>>>>     element2::T2
>>>> end
>>>>
>>>> If no special constructor is defined, as in the case above, Julia 
>>>> provides constructors such that I can create:
>>>> - pair objects of specified T1 and T2, for example 
>>>> Pair{Int64,Float64}(a,b), provided that a is some Int64 and b is a Float64
>>>> - pair objects where T1 and T2 are automatically inferred from the 
>>>> arguments, i.e. Pair(a,b) will result in an object of type 
>>>> Pair{typeof(a),typeof(b)}
>>>>
>>>> If I define a special inner constructor to replace the constructor of 
>>>> the first line, I also have to provide an outer constructor that replaces 
>>>> the functionality of the second line. Is my understanding so far correct?
>>>>
>>>> Now I add some aliases to the game. If I define
>>>> typealias IntPair Pair{Int,Int}
>>>>
>>>>
>>>> then Julia automatically provides a constructor such that I can do
>>>> IntPair(a,b), which will create an object of type Pair{Int,Int} 
>>>> provided that a and b are integers
>>>>
>>>> Now I define a parametric typealias, such as
>>>> typealias EqualPair{T} Pair{T,T}
>>>>
>>>>
>>>> then Julia does provide a constructor if I specify the type, i.e. 
>>>> EqualPair{Int}(a,b) will work if a and b are integers, and returns an 
>>>> object of type Pair{Int,Int}. However, there is no automatic interference 
>>>> of the type T if I do not specify it:
>>>> EqualPair(a,b) returns 
>>>> "ERROR: type: apply: expected Function, got Type{Pair{T,T}}"
>>>>
>>>> And now comes my question: If I now try to define the more general 
>>>> constructor myself, by writing either
>>>> EqualPair{T}(a::T,b::T)=Pair{T,T}(a,b)
>>>> or
>>>> EqualPair{T}(a::T,b::T)=EqualPair{T}(a,b)
>>>> I get, in both cases, an error:
>>>> "ERROR: invalid method definition: not a generic function"
>>>>
>>>> How should I then define a constructor for EqualPair that is more 
>>>> general than the one provided by Julia?
>>>>
>>>
>

Reply via email to