Declaration
Function names must begin with a lowercase letter.
No parameters, no return
fun say_hello()
{
print("Hello")
}
say_hello()
thp
With return type
fun get_random_number() -> Int
{
Random::get(0, 35_222)
}
val number = get_random_number()
thp
Syntax error: There should be an identifier after a binding at line 3:11
With parameters and return type
fun get_secure_random_number(Int min, Int max) -> Int
{
Random::get_secure(min, max)
}
val number = get_secure_random_number(0, 65535)
thp
Syntax error: There should be an identifier after a binding at line 3:11
Generic types
fun get_first_item[T](Array[T] array) -> T
{
array[0]
}
val first = get_first_item[Int](numbers)
// The type annotation is optional if the compiler can infer the type
val first = get_first_item(numbers)
thp
Syntax error: Expected an opening paren after the function identifier. at line 1:18
Signature
() -> ()
() -> (Int)
(Int, Int) -> (Int)
[T](Array[T]) -> (T)
thp
Syntax error: Expected an statement or an expresion at the top level. at line 1:0
Named arguments
fun html_special_chars(
String input,
Int? flags,
String? encoding,
Bool? double_encoding,
) -> String
{
// ...
}
html_special_chars(input, double_encoding: false)
thp
Syntax error: Expected an identifier for the parameter. at line 3:1
TBD: If & how named arguments affect the order of the parameters
Named arguments with different names
fun greet(
String name,
String from: city,
)
{
print("Hello {name} from {city}!")
}
greet("John", from: "LA")
thp
Syntax error: Expected a closing paren after the function identifier. at line 3:16