[Grace-core] Polymorphism over unit

Michael Homer mwh at ecs.vuw.ac.nz
Mon Nov 28 18:20:38 PST 2011


Hi,
I am trying to write code that is polymorphic over whether
Unit(/Void/...) is returned or not. It's specifically for the pattern
matching implementation, but it could come up elsewhere as well.

The issue arises when returning the result of applying an arbitrary
block, or some other signal value. When the block returns a normal
value, that works fine and has a well-defined disjunctive type R | S.
Testing the result for equality with the signal value then isn't a
problem. If the block happens to return Unit, there is a problem,
because the Unit value can't be assigned anywhere, or be an argument
to another method.

Concretely:
  method match(value) case(block<A,ReturnType>) -> ReturnType |
MatchFailed { ... return block.apply(value) ... return MatchFailed }
  def m1 = match(-1)case { print "A negative number!" }
  if (MatchFailed == m1) then { ... }
The assignment statement fails, either statically or at runtime, and
there's no way around it. The unit value also can't be encapsulated in
another object to return for the same reason. What's the solution
here?
-Michael


More information about the Grace-core mailing list