refactor: latest docs

This commit is contained in:
Fernando Araoz 2025-01-20 22:06:16 -05:00
parent c26251567c
commit a1641046cb
12 changed files with 68 additions and 89 deletions
src/pages/en/latest/learn
02_data-structures
03_flow-control
04_functions
05_error-handling

View File

@ -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.
<Code thpcode={`
enum Suit
{
enum Suit {
Hearts,
Diamonds,
Clubs,
@ -36,8 +35,7 @@ Backed enums can have a scalar for each case. The scalar values can only be
<Code
thpcode={`
enum Suit(String)
{
enum Suit(String) {
Hearts = "H",
Diamonds = "D",
Clubs = "C",

View File

@ -10,8 +10,7 @@ import Code from "@/components/Code.astro";
Tagged unions can hold a value from a fixed selection of types.
<Code thpcode={`
union Shape
{
union Shape {
Dot,
Square(Int),
Rectangle(Int, Int),
@ -27,12 +26,10 @@ val rectangle1 = Shape::Rectangle(5, 15)
<Code
thpcode={`
match shape_1
case ::Square(side)
{
case ::Square(side) {
print("Area of the square: {side * side}")
}
case ::Rectangle(length, height)
{
case ::Rectangle(length, height) {
print("Area of the rectangle: {length * height}")
}
`}
@ -47,8 +44,7 @@ are contained as part of an array.
```php
// The first snippet is compiled to:
enum Shape
{
enum Shape {
case Dot;
case Square;
case Rectangle;

View File

@ -18,7 +18,6 @@ val result = {
val temp = 161
temp * 2 // This will be assigned to \`result\`
}
print(result) // 322

View File

@ -17,8 +17,7 @@ The following code shows a function without parameters
and without return type:
<Code thpcode={`
fun say_hello()
{
fun say_hello() {
print("Hello")
}
@ -34,8 +33,7 @@ type is **mandatory**, and it's done by placing an
arrow `->` followed by the return datatype:
<Code thpcode={`
fun get_random_number() -> Int
{
fun get_random_number() -> Int {
return Random::get(0, 35_222)
}
@ -47,8 +45,7 @@ a return type:
<Code
thpcode={`
fun get_random_number()
{
fun get_random_number() {
// Error: the function does not define a return type
return Random::get(0, 35_222)
}
@ -60,8 +57,7 @@ expression on the function:
<Code
thpcode={`
fun get_random_number() -> 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.
<Code thpcode={`
fun add(Int a, Int b) -> 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:
<Code thpcode={`
fun get_first_item[T](Array[T] array) -> 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}!")
}

View File

@ -10,8 +10,7 @@ import Code from "@/components/Code.astro";
## Function as parameters
<Code thpcode={`
fun map[A, B](Array[A] input, (A) -> (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
<Code thpcode={`
fun generate_generator() -> () -> Int
{
fun generate_generator() -> () -> Int {
// code...
return fun() {
322

View File

@ -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?`.
<Code
thpcode={`
?String new_username = POST::get("username")
String? new_username = POST::get("username")
`}
/>
@ -26,14 +26,12 @@ check if the value is null, and then use it.
The syntax `?` returns `true` if the value is not null.
<Code thpcode={`
if new_username?
{
if new_username? {
// Here \`new_username\` is automatically casted to String
}
// you can also manually check for null
if new_username == null
{
if new_username == null {
// This is the same as above
}
@ -49,24 +47,24 @@ To create a nullable type we must explicitly annotate the type.
<Code thpcode={`
val favorite_color = null // Error, we must define the type
?String favorite_color = null // Ok
String? favorite_color = null // Ok
`} />
Other examples:
<Code thpcode={`
fun get_first(Array[?String] values) -> ?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 `?.`.
<Code thpcode={`
?Person person = ...
Person? person = ...
val name = person?.name
`} />
@ -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.
<Code thpcode={`
?String lastname = find_lastname()
String? lastname = find_lastname()
// Tell the compiler trust me,
// I know this is not null
@ -109,7 +107,7 @@ The Elvis operator `??` is used to give a default value in case a `null` is foun
<Code thpcode={`
// This is a function that may return a Int
fun get_score() -> ?Int {...}
fun get_score() -> Int? {...}
val test_score = get_score() ?? 0
`} />

View File

@ -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:
<Code thpcode={`
fun invert(Int number) -> 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 `!`.
<Code
thpcode={`
fun sample() -> 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.
<InteractiveCode
code={`
fun dangerous() -> 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}")