Data Types in Verse
The type macro is a special Verse construct that can be used to define functions or construct new types from existing types using where clauses. The type macro can be used anywhere a type can be used. For example, the following example constructs a new type using a where clause, then uses that type in a function:
| | |
| --- | --- |
| | # Construct a new type with a where clause |
| | uint8 := type{X:int where 0 <= X, X < 256} |
| | |
| | # Define a new function using uint8 |
| | Foo():uint8 = 0 |
Construct a new type with a where clause
uint8 := type{X:int where 0 <= X, X < 256}
Define a new function using uint8
Foo():uint8 = 0
Copy full snippet(5 lines long)
There where clause is only supported in function parameter definitions or as a range condition on numeric types.
The following example uses the type macro to define a function as an argument to a new function:
| | |
| --- | --- |
| | # Define a function with type |
| | Bar(X:type{_():uint8}):uint8 = X() |
| | |
| | # Call Bar with Foo argument |
| | Y := Bar(Foo) |
Define a function with type
Bar(X:type{_():uint8}):uint8 = X()
Call Bar with Foo argument
Y := Bar(Foo)
You're reading a preview
The full reference is free for BrainDeadGuild Discord members — sign in to read it all, or open the original at the source.
Sign in with your BrainDead.TV / BrainDeadGuild Discord account for full access.