Code:Signature
A signature is a compact way of representing the types a function takes, and the types it outputs.
Form
Generally, a signature is of the form:
(arg1: Type1, arg2: type2, arg3: Type3, ... argN: typeN) -> ReturnType
With the name of the argument being separated from the type with a colon (:
).
Modifiers
A type is not always on its own, sometimes it will have a modifier.
?
A question mark represents that an argument is optional, and doesn't have to be specified. For example, in the signature for rand-int:
(min: Number?, max: Number) -> Number
the argument min
is optional. This means that we can leave it out, and call the function with either one or two arguments.
...
An ellipsis represents that a function takes zero or more of a given type. For example, in the signature for send-message:
(player: Player, messages: Any...) -> Unit
We must give the argument player
, however after that, we can choose to put as many values as we want.
|
A pipe is placed inbetween two types, and denotes that it may be one type or another. For example, in the signature for player-by-name:
(name: String) -> Player | Unit
We see pipe being used. This means that this function could either return a Player or Unit.