frroossst / gsuneido

Go implementation of Suneido
MIT License
0 stars 0 forks source link

Tagged unions #3

Open frroossst opened 1 year ago

frroossst commented 1 year ago

It's no secret I love them, see if Suneido can natively support tagged unions or make a similar implementation with classes.

enum DataType {
    Integer,
    Float,
    String
};

struct Data {
    enum DataType type;
    union {
        int intValue;
        float floatValue;
        char* stringValue;
    } value;
};
frroossst commented 1 year ago

This could just be a wrapper around a Suneido object

frroossst commented 1 year ago

Sample class to emulate tagged unions.

result = class
    {
    New()
        {
        .err = false
        .val = false
        }
    Ok(.val)
        {   
        .err = false
        }
    Err(.err)
        {   
        .val = false
        }
    Unwrap()
        {
        if .err isnt false
            return throw .err
        return .val
        }
    }
result = result()
result.Ok(123)
a = result.Unwrap()
Print(:a)
result.Err("uhhh")
a = result.Unwrap()
Print(:a)