[Grace-core] Types of blocks

Michael Homer mwh at ecs.vuw.ac.nz
Tue Aug 23 14:09:09 PDT 2011


On Tue, Aug 23, 2011 at 1:11 PM, Kim Bruce <kim at cs.pomona.edu> wrote:
> In general you would take the sup of the return types (which happily exists in a structurally typed language).  The return type of a block is the type of the last statement in the block (treating a stand-alone expression as a statement).
>
> Most commands will have type Unit.  E.g., the statement x:= 12 will have type Unit, not Number.  Similarly, a bare return statement will return a value of type Unit.  Our interpreter doesn't actually have return statements.

Concretely: which of these blocks have the same type?
  { a : String, b : Boolean, c : Boolean -> if (b) then { a } else { a.size } }
  { a : String, b : Boolean, c : Boolean -> if (c) then { return a }
elseif (b) then { a } else { a.size } }
  { a : String, b : Boolean, c : Boolean -> if (c) then { b := c }
elseif (b) then { a } else { a.size } }
  { a : String, b : Boolean, c : Boolean -> if (c) then { return a } ;
if (b) then { a } else { a.size } }
The second has the possibility of not returning anything at all. The
third may return Unit. The fourth may terminate before the last
statement.

If blocks can have generic parameters at all, the return type has to
be a type union, not sup, or else it's just the top type.
-Michael


More information about the Grace-core mailing list