-
-
Notifications
You must be signed in to change notification settings - Fork 4
optional
To avoid null errors Angle offers the swift way
class car{
name:string // default
name:string! // required
year:date? //optional
year?:date //optional flow syntax
?year:date //ternary syntax conflict
brand? //optional with type name matching
}
Semantically optional types (should) always belong to variables or parameters: It makes sense that someone is maybe a person, but less so to say that someone is a "maybe-person".
Fields can be made required in function declarations via with
keyword or nested schemas:
age of car with year = car.year
age of car{year} = car.year
The scheme of the type can be adhoc and deep within function declarations:
arrival time of package{destination{address{zip or street}}} = calculate distance to zip …
To avoid redundancy, the type scheme leafs act as local symbols, so in the above example 'zip' is available to the block.
The untyped variant would just be
arrival time of package is calculate distance to its zip
Relying on deep accessor indexing
make optionally as unknown
default but enable way to specify the requirement of fields:
to get movie time of user with name and zip:...
to place order for user with last name and address:...
Julia style
movie-time (user:{name, address: zip} ) = ...
Nested optionality thanks to Rick Hickey from closure
These required attributes can also be inferred from the compiler:
peter{last-name=fox zip=1234}
movie-time(peter) // compiler warning/error: movie time requires peter to have a name
// extra: did you mean lastname?
Related: Nullable Types and Typed Null.
Errors and exceptions in angle are tightly linked with the concept of optionality in variables:
The function parse string as int?
either returns an int or a type "missing int" / typed error !int
.
All missing / error types/value have an associated (?todo) type format error or number to large etc
The result can be handled positively:
if result { print "we have a number %d" % result }
if no result { print "parse did not return a number" }
if result failed { print "we have an error %e" % result }
or
if result failed { print "we have an error %s" % result.error }
on error { print "we have an error %e" % error }
Just like in swift optionals can be unwrapped with tailing !
which throws if the variables value is missing or erroneous.
Note that no result
is true both for missing and erroneous objects, which might be sufficient for most control flow, otherwise one can distinguish with result == 0
== result null
result missing
result empty
and result failed
See monad for abstract extension and pipe for simple syntax.