Errata : here is a MWE that actually works.

immutable Literal
    symbol::Symbol
    level::Int
end
typealias SymbolInt @compat(Tuple{Symbol,Int})
Literal(tup::SymbolInt) = Literal(tup[1], tup[2])
Literal(sym::Symbol) = Literal(sym, 1)

typealias MyList LinkedList{Literal}
MyList() = nil(Literal)
MyList(head, tail...) = cons(Literal(head), MyList(tail...))



After running this on v0.4, MyList(:a, (:b, 2)) yields
list(Literal(:a,1), Literal(:b,2))
which is exactly what I want: a list of Literals that support mixed syntax 
for element construction.
Is it possible to do this on prior versions ?

Vincent.

Le lundi 27 juillet 2015 19:45:52 UTC+2, Vincent Lostanlen a écrit :
>
> Is there a way to make this backwards compatible with latest v0.3 ? I find 
> typealias constructors extremely handy to extend the behavior of existing 
> recursive constructors (LinkedList here).
> Here is a minimal use case requiring DataStructures and Compat:
>
> immutable Literal
>     symbol::Symbol
>     level::Int
> end
> typealias SymbolInt @compat(Tuple{Symbol,Int})
> Literal(tup::SymbolInt) = Literal(tup[1], tup[2])
> Literal(sym::Symbol) = Literal(sym, 1)
>
> typealias MyList LinkedList{Literal}
> MyList() = nil(Literal)
> MyList(head::Literal, tail...) = cons(head, VariableKey(tail...))
> MyList(head::Union(SymbolInt,Symbol), tail...) = cons(Literal(head), 
> VariableKey(tail...))
>
> On v0.4, this code makes the MyList type a synonym of LinkedList{Literal}, 
> without having to explicitly construct a Literal object for every element 
> in the list a priori.
> What can I do ?
>
> Vincent.
>
> Le samedi 2 mai 2015 20:43:58 UTC+2, Mauro a écrit :
>>
>> You need to be on 0.4 to do this. 
>>
>> On Sat, 2015-05-02 at 20:40, Krishna Subramanian <[email protected]> 
>> wrote: 
>> > I would like to know how to do something like this- 
>> > typealias Corpus Array{Int,1} 
>> > 
>> > and write a constructor for it. Currently, if I do - 
>> > 
>> > Corpus() = Array(Int,0) 
>> > 
>> > I get- 
>> > 
>> > ERROR: cannot define function Corpus; it already has a value 
>> > 
>> > I am sure others have encountered this problem before. I went through 
>> > https://github.com/JuliaLang/julia/issues/1470 but I am still not able 
>> to 
>> > figure out what is the issue involved. 
>> > 
>> > Thanks, 
>> > Krishna 
>>
>>

Reply via email to