spool-lang / Spool-Legacy-Repo

Legacy repo for the Spool programming language.
3 stars 0 forks source link

None and Unknown Types #15

Closed RedstoneParadox closed 5 years ago

RedstoneParadox commented 5 years ago

The None type in silicon has two purposes. One of them, as mentioned in #5: Functions is to signify that a function does not return anything. The other purpose of the None type is to be one of two possible results of an unknown type. The unknown type is a type identifier followed by the ? symbol. An unknown type can either have a value of the specified type or None as shown below:


class Foo {

}

class Bar {

var unknownFooOne : Foo? = None
var unknownFooTwo : Foo? = new Foo()

}

Since the value of any var or const with an unknown type is not guaranteed to be a specific type, it needs to be verified that it is either the concrete type or None before calling any functions or using any operators on it with the exception of the is operator.

main {

    var maybe : Foo? = functionThatMightReturnAFoo()

    if (maybe !is Foo)
    {
        //Code
    }

    /*
    Here, this function can't be called despite the previous if statement because this code runs 
    regardless of whether or not maybe is of type Foo.
    */
    maybe.doFooThing()

    if (maybe is Foo)
    {
        //Here, we can call this function since this code only runs if maybe is a Foo and not a None.
        maybe.doFooThing()
    }

}