very handy, just saw this reply now. thanks a ton
On Thursday, November 6, 2014 at 5:16:31 PM UTC-5, Valentin Churavy wrote: > > You could using a abstract type instead of a Union > > abstract Element > type Tree > body :: Element > end > > type Branch <: Element > a :: Tree > b :: Tree > end > > type Leaf <: Element > a > end > > so this would create a tree > julia> Tree(Branch( > Tree(Leaf(:a)), > Tree(Branch( > Tree(Leaf(:b)), > Tree(Leaf(:c)) > )) > )) > Tree(Branch(Tree(Leaf(:a)),Tree(Branch(Tree(Leaf(:b)),Tree(Leaf(:c)))))) > > adding the following methods makes it a bit more readable > > Tree(a :: Any) = Tree(Leaf(a)) > Tree(a :: Tree,b::Tree) = Tree(Branch(a, b)) > > julia> Tree( > Tree(:a), > Tree( > Tree(:b), > Tree(:c) > ) > ) > Tree(Branch(Tree(Leaf(:a)),Tree(Branch(Tree(Leaf(:b)),Tree(Leaf(:c)))))) > > > So this stills looks a bit clunky and you should also be aware that this > allows for Tree(Tree(:a), Tree(1.0)) so some type constraints would be in > order. > > > On Thursday, 6 November 2014 21:52:05 UTC+1, Evan Pu wrote: >> >> Quick question: >> >> In haskell one can do something like the following to define a type: >> >> data Tree a = Branch (Tree a) (Tree a) | Leaf a >> >> >> Is there something analogous in the Julia world? >> I'm sure I'm doing something wrong here... >> >> julia> type Tree >> body :: Union(Branch, Leaf) >> end >> ERROR: Branch not defined >> >> julia> type Branch >> a :: Tree >> b :: Tree >> end >> ERROR: Tree not defined >> >> julia> type Leaf >> a >> end >> >> thanks! >> >
