diff --git a/src/pages/en/latest/learn/02_data-structures/enums.mdx b/src/pages/en/latest/learn/02_data-structures/enums.mdx index d6dfb49..87dcad8 100644 --- a/src/pages/en/latest/learn/02_data-structures/enums.mdx +++ b/src/pages/en/latest/learn/02_data-structures/enums.mdx @@ -18,8 +18,7 @@ THP enums are a 1 to 1 map of PHP enums, with a slightly different syntax. Enums don't have a scalar value by default. diff --git a/src/pages/en/latest/learn/02_data-structures/unions.mdx b/src/pages/en/latest/learn/02_data-structures/unions.mdx index 4d78a1b..2dbb1dd 100644 --- a/src/pages/en/latest/learn/02_data-structures/unions.mdx +++ b/src/pages/en/latest/learn/02_data-structures/unions.mdx @@ -10,8 +10,7 @@ import Code from "@/components/Code.astro"; Tagged unions can hold a value from a fixed selection of types. 0 { -print("user_id is greater than 0: {user_id}") + print("user_id is greater than 0: {user_id}") } `} /> diff --git a/src/pages/en/latest/learn/03_flow-control/loops.mdx b/src/pages/en/latest/learn/03_flow-control/loops.mdx index adb4280..cbb7879 100644 --- a/src/pages/en/latest/learn/03_flow-control/loops.mdx +++ b/src/pages/en/latest/learn/03_flow-control/loops.mdx @@ -19,7 +19,7 @@ Braces are required. val numbers = [0, 1, 2, 3] for number in numbers { -print(number) + print(number) } `} /> @@ -31,7 +31,7 @@ val dict = .{ } for value in dict { -print("{value}") + print("{value}") } `} /> @@ -41,7 +41,7 @@ print("{value}") val numbers = [0, 1, 2, 3] for index, number in numbers { -print("{index} : {number}") + print("{index} : {number}") } `} /> @@ -53,7 +53,7 @@ val dict = .{ } for key, value in dict { -print("{key} => {value}") + print("{key} => {value}") } `} /> @@ -64,8 +64,8 @@ val colors = ["red", "green", "blue"] var index = 0 while index < colors.size() { -print("{colors[index]}") -index += 1 + print("{colors[index]}") + index += 1 } `} /> diff --git a/src/pages/en/latest/learn/03_flow-control/match.mdx b/src/pages/en/latest/learn/03_flow-control/match.mdx index 7a73f32..1447a71 100644 --- a/src/pages/en/latest/learn/03_flow-control/match.mdx +++ b/src/pages/en/latest/learn/03_flow-control/match.mdx @@ -20,36 +20,36 @@ case None { print("user_id doesn't exist") } match user_id case Some(id) { -print("user_id exists: {id}") + print("user_id exists: {id}") } case None { -print("user_id doesn't exist") + print("user_id doesn't exist") } match user_id case Some(id) if id > 0 { -print("user_id exists: {id}") + print("user_id exists: {id}") } else { -print("user_id has other values ") + print("user_id has other values ") } match customer_id case 1, 2, 3 { -print("special discount for our first 3 customers!") + print("special discount for our first 3 customers!") } else { -print("hello dear") + print("hello dear") } match customer*id | 1 | 2 | 3 { -print("ehhhh") + print("ehhhh") } | 4 | 5 { -print("ohhh") + print("ohhh") } | * { -print("???") + print("???") } `} /> diff --git a/src/pages/en/latest/learn/04_functions/declaration.mdx b/src/pages/en/latest/learn/04_functions/declaration.mdx index 3450898..f2495e9 100644 --- a/src/pages/en/latest/learn/04_functions/declaration.mdx +++ b/src/pages/en/latest/learn/04_functions/declaration.mdx @@ -17,8 +17,7 @@ The following code shows a function without parameters and without return type: ` followed by the return datatype: Int -{ +fun get_random_number() -> Int { return Random::get(0, 35_222) } @@ -47,8 +45,7 @@ a return type: Int -{ +fun get_random_number() -> Int { // The last expression of a function is // automatically returned Random::get(0, 35_222) @@ -75,8 +71,7 @@ Parameters are declared like C-style languages: `Type name`, separated by commas. Int -{ +fun add(Int a, Int b) -> Int { return a + b } @@ -96,8 +91,7 @@ The following example declares a generic `T` and uses it in the parameters and return type: T -{ +fun get_first_item[T](Array[T] array) -> T { array[0] } @@ -134,8 +128,7 @@ fun html_special_chars( Int? flags, String? encoding, Bool? double_encoding, -) -> String -{ +) -> String { // ... } @@ -150,8 +143,7 @@ TBD: If & how named arguments affect the order of the parameters fun greet( String name, String from: city, -) -{ +) { print("Hello {name} from {city}!") } diff --git a/src/pages/en/latest/learn/04_functions/higher-order.mdx b/src/pages/en/latest/learn/04_functions/higher-order.mdx index 520e024..68bdf4e 100644 --- a/src/pages/en/latest/learn/04_functions/higher-order.mdx +++ b/src/pages/en/latest/learn/04_functions/higher-order.mdx @@ -10,8 +10,7 @@ import Code from "@/components/Code.astro"; ## Function as parameters (B) function) -> Array[B] -{ +fun map[A, B](Array[A] input, (A) -> (B) function) -> Array[B] { // implementation } @@ -20,8 +19,7 @@ fun map[A, B](Array[A] input, (A) -> (B) function) -> Array[B] ## Function as return () -> Int -{ +fun generate_generator() -> () -> Int { // code... return fun() { 322 diff --git a/src/pages/en/latest/learn/04_functions/lambdas.mdx b/src/pages/en/latest/learn/04_functions/lambdas.mdx index 9e6949d..220ac89 100644 --- a/src/pages/en/latest/learn/04_functions/lambdas.mdx +++ b/src/pages/en/latest/learn/04_functions/lambdas.mdx @@ -64,7 +64,7 @@ By default closures **always** capture variables as **references**. var x = 20 val f = fun() { -print(x) + print(x) } f() // 20 @@ -87,7 +87,7 @@ fun(parameters) clone(variables) { var x = 20 val f = fun() clone(x) { -print(x) + print(x) } f() // 20 @@ -112,6 +112,6 @@ numbers.map() #{ // the above lambda is equivalent to: numbers.map(fun(param1) { -$1 \* 2 + $1 \* 2 }) `} /> diff --git a/src/pages/en/latest/learn/05_error-handling/null.mdx b/src/pages/en/latest/learn/05_error-handling/null.mdx index 724e23f..bb64a4f 100644 --- a/src/pages/en/latest/learn/05_error-handling/null.mdx +++ b/src/pages/en/latest/learn/05_error-handling/null.mdx @@ -12,11 +12,11 @@ To represent `null` we must use nullable types, represented by the question mark `?` character. For instance, a POST request may have a `username` parameter, -or it may not. This can be represented with an `?String`. +or it may not. This can be represented with an `String?`. @@ -26,15 +26,13 @@ check if the value is null, and then use it. The syntax `?` returns `true` if the value is not null. @@ -49,24 +47,24 @@ To create a nullable type we must explicitly annotate the type. Other examples: ?String {} +fun get_first(Array[String?] values) -> String? {} val result = get_first([]) `} /> ## Optional chaining -If you have a `?Type` and you wish to access a field of `Type` if it exists, +If you have a `Type?` and you wish to access a field of `Type` if it exists, you can use the optional chaining operator `?.`. @@ -76,14 +74,14 @@ val name = person?.name ## Null unboxing -The `!!` operator transforms a `?Type` into `Type`. +The `!!` operator transforms a `Type?` into `Type`. If you are sure that a value cannot be `null`, you can force the compiler to treat it as a regular value with the `!!` operator. Note the two exclamation marks. ?Int {...} +fun get_score() -> Int? {...} val test_score = get_score() ?? 0 `} /> diff --git a/src/pages/en/latest/learn/05_error-handling/try.mdx b/src/pages/en/latest/learn/05_error-handling/try.mdx index cb5de95..acbb570 100644 --- a/src/pages/en/latest/learn/05_error-handling/try.mdx +++ b/src/pages/en/latest/learn/05_error-handling/try.mdx @@ -13,42 +13,41 @@ and handled. ## Declare that a function returns an exception -Possible errors have their own syntax: `Error!Type`. -This means: This may be an `Error`, or a `Type`. +Possible errors have their own syntax: `Type!Error`. +This means: This may be a `Type`, or an `Error`. For example, a function that returned a `DivisionByZero` may be written like this: DivisionByZero!Int -{ +fun invert(Int number) -> Int!DivisionByZero { if number == 0 { - return DivisionByZero() + throw DivisionByZero() } return 1 / number - } `} /> -In the previous segment, `DivisionByZero!Int` denotates +In the previous segment, `Int!DivisionByZero` denotates that the function may return either a `DivisionByZero` error or an `Int`. -There is no `throw` keyword, errors are just returned. +The `throw` keyword is used to denotate that an error is being +returned. ### Multiple error returns TODO: properly define syntax, how this interacts with type unions. -Multiple errors are chained with `!`. The last one is always -the success value. +Multiple errors are chained with `!`. Error1!Error2!Error3!Int -{ /* ... */} +fun sample() -> Int!Error1!Error2!Error3 { + /* ... */ +} `} /> @@ -66,14 +65,13 @@ Use a naked `try` when you want to rethrow an error, if there is any. Exception!Int - { // May throw randomly + fun dangerous() -> Int!Exception { // May throw randomly return if Math.random() < 0.5 { 50 } else { Exception("Unlucky") } } - fun run() -> Exception! - { // If \`dangerous()\` throws, the function exits with the same error. + fun run() -> Void!Exception { + // If \`dangerous()\` throws, the function exits with the same error. // Otherwise, continues val result = try dangerous() print("The result is {result}")