Sum types are now more powerful

Published May 06, 2012
Advertisement
I worked on the sum type implementation for a while, and finally got it up to the point where you can dynamically alter the type of data stored in a sum-typed variable. This is subtly different from dynamic typing; the possible types of the variable are bounded at compile time, and the compiler guarantees that you can always act on the data at run time regardless of what type it actually ends up being.

Bottom line, this test now works:

//
// SUMTYPESTRUCTURE.EPOCH
//
// Test of holding a sum-typed value in a structure member
//


type intorstring : integer | string

structure s :
intorstring alpha,
intorstring beta


operate : s param
{
check(param.alpha)
check(param.beta)
}


check : integer param
{
assert(param == 42)
}

check : string param
{
assert(param == "test")
}


entrypoint :
{
s foo = 42, "test"
s bar = "test", 42
s baz = 42, 42
s quux = "test", "test"

operate(foo)
operate(bar)
operate(baz)
operate(quux)

print("Pass")
}



Next up I'm going to give a shot at implementing a dynamic list structure using this functionality. Once that works I'll probably start on templates/generics to get the compiler to the point where I can implement much more interesting stuff without repeating boilerplate code... things like containers, and so on.

Should be interesting!
1 likes 1 comments

Comments

Servant of the Lord
Craziness. You're going to get us killed when the universe collapses because of your meddlings. =)
You're lucky we're no longer in the dark ages, or 'purist' programmers would strap you to a server and burn you alive.

Keep up the crazy stuff you are doing! I find it all very interesting.
May 07, 2012 12:52 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement