myst-lang / myst

A structured, dynamic, general-purpose language.
http://myst-lang.org
MIT License
119 stars 17 forks source link

Implement Method Return Types #199

Closed faultyserver closed 6 years ago

faultyserver commented 6 years ago

This is work that I did a number of months ago and have completely forgotten if it was finished or not. However, looking at the diff and seeing the tests all pass, it seems mostly complete to me, so I'm creating this PR.

In general, this PR does 4 things:

Importantly, this PR does not implement return type checking. That is, specifying a return type on a method does not actually cause anything to happen in the interpreter. It is purely decorative for now and relies on developers honoring their function signatures manually. Eventually, this will get used by https://github.com/faultysever/myst-typecheck for pre-execution type checking, and will be used by the interpreter to enforce that those restrictions are satisfied at runtime.

bmulvihill commented 6 years ago

@faultyserver Unions don't appear to be working correctly, unless my syntax is wrong here:

deftype Test
  def initialize
  end

  def union(x : String | Integer)
    STDOUT.puts(x)
  end
end

test = %Test{}

test.union("hi")
test.union(1)

produces: Uncaught Exception: No clause matches with given arguments: ["hi"]

faultyserver commented 6 years ago

@bmulvihill Your code is correct and should work. The Matcher needs to be updated to handle Unions as patterns.

faultyserver commented 6 years ago

@bmulvihill The latest commits should add the necessary support in the interpreter for your example to work.