Closed faultyserver closed 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"]
@bmulvihill Your code is correct and should work. The Matcher
needs to be updated to handle Unions as patterns.
@bmulvihill The latest commits should add the necessary support in the interpreter for your example to work.
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:
Object
for representing instances rather than just static Types.Type
is now a subclass ofObject
, and any type definitions will inherit from it, while instances will not.TypeA | TypeB
. Anywhere a type restriction is given, a union should be able to take its place.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.