[Grace-core] Aside from Abstract

Kim Bruce kim at cs.pomona.edu
Tue Nov 5 11:44:27 PST 2013


The other thing we decided yesterday beyond having an "abstract" statement to indicate an abstract class is to have a singleton operator.  Singleton takes an identifier defined in a definition and makes it into a type.

def emptyList = object {....}

type EmptyList = singleton emptyList

Now EmptyList has the pattern matching semantics expected of a type and can be used in match cases, etc.

Any object can be used in a match by providing a match method, but that method may not at all conform to the definition expected for a singleton type.  The advantage of singleton is that it provides you automatically with a match method that does exactly what is expected for the singleton type, no matter what the object had (or even if it had no match).

var x: EmptyList | List
....

match (x)
     case{x:EmptyList -> print "I'm empty"}
     case(l:List -> print "I have a head: {l.head}, and tail: {l.tail}"}

Kim






More information about the Grace-core mailing list