mfichman / jogo

High(er) performance compiled programming language with clean, powerful syntax
MIT License
6 stars 1 forks source link

Add union types #23

Closed mfichman closed 12 years ago

mfichman commented 12 years ago

Similar to Haskell data structures. For example, a union type could be:

Json::Value = Bool | Int | Float | String | Array[Json::Value] | Map[Json::Value]

A union type is a subtype of any of the types in the union. Also, any type in the union is a subtype of the union type. Thus assignments such as this are legal:

val Json::Value = true
int Int = val

If 'val' is not the requested type, nil or the default value is used. Also, the is operator should be implemented:

if val is Bool {
    bool Bool = val
}

The is operator returns true if the tag is equal to the requested type. Every union has a tag that is equal to the vtable for the requested type:

struct Union {
    Ptr _vtable;
    Ptr _refcount;
    T _val ?
};

If the Union is a primitive or value, then the value is embedded directly into _val. If Union is a reference object, Union is just a pointer to the reference object. When packing/unpacking Unions, the _vtable field is examined to determine the type. So an assignment with a Union looks like this (in C):

int Int = (val._vtable == Int__vtable) ? val_.val : 0;
string String = (val._vtable == String__vtable) ? val : 0;

Union types are reference objects.

mfichman commented 12 years ago

Added any types, which is the first step in this process. Need to add boxing and type checking for unions.

mfichman commented 12 years ago

Complete.