-
Notifications
You must be signed in to change notification settings - Fork 1
Option
justinmann edited this page Dec 24, 2017
·
2 revisions
Instead of null values, SJ uses option instead. Options can be empty or have a value. They have a few special operations to make it easy to use them. They are considered a special type, an option type cannot be passed to a normal type and vice versa. For instance, 'i32' is a normal type and 'i32?' is an option type.
class(
x : empty'i32
y : 0
) { this }
i : empty'i32
c : empty'class
d : value(class()) // creates a class and changes type from 'class' to 'class?'
isEmpty(c) // returns true
isEmpty(d) // returns false
c?.x // returns empty'32 because c is empty
d?.x // returns empty'32 because d.x is empty
d?.y // returns 0
e : c ?: class() // if c is empty then create a new class
ifValid c {
c.y.asString() // you can use 'c' without the ?. because it is inside an ifValid block
} elseEmpty {
"oh well"
}
ifValid alpha : c, beta : d {
// same as above, but with multiple options
// and they get different name inside this scope
}