Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alias using generic types #3049

Closed
pbrusco opened this issue Jul 26, 2016 · 2 comments
Closed

alias using generic types #3049

pbrusco opened this issue Jul 26, 2016 · 2 comments

Comments

@pbrusco
Copy link

pbrusco commented Jul 26, 2016

Is it possible to define generic aliases?

I would like to have something like alias AB(T) = T | Tuple(AB(T), AB(T)) to represent Haskell like, binary trees.

@pbrusco
Copy link
Author

pbrusco commented Jul 26, 2016

Example I want to generalize:

# alias AB(T) = T | Tuple(AB(T), AB(T))
alias ABInt = Int32 | Tuple(ABInt, ABInt)

def fold(e : ABInt, fLeaf : Int32 -> T, fRec : Proc(T, T, T)) : T
  case e
  when Int32
    fLeaf.call(e)
  when Tuple(ABInt, ABInt)
    rec1 = fold(e.fst, fLeaf, fRec)
    rec2 = fold(e.snd, fLeaf, fRec)
    fRec.call(rec1, rec2)
  else
    raise "type Error"
  end
end

struct Tuple
  def fst
    self[0].not_nil!
  end

  def snd
    self[1].not_nil!
  end
end

puts fold( {1, {2, 3}}, ->(n : Int32){n}, ->(reci : Int32, recd : Int32){reci + recd } )

@asterite
Copy link
Member

Duplicate of #2803 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants