diff --git a/md/learn/adts/union-types.md b/md/learn/adts/union-types.md index b66dc7b..1ae7215 100644 --- a/md/learn/adts/union-types.md +++ b/md/learn/adts/union-types.md @@ -24,7 +24,7 @@ enum IpAddress V6(String), } -let addr_1 = IpAddress::V4("192.168.0.1") +val addr_1 = IpAddress::V4("192.168.0.1") match addr_1 | IpAddress::V4(ip) diff --git a/md/learn/basics/variables.md b/md/learn/basics/variables.md index 656b37d..420fdd5 100644 --- a/md/learn/basics/variables.md +++ b/md/learn/basics/variables.md @@ -4,23 +4,23 @@ thp distinguishes between mutable and immutable variables. ## Immutable variables -Defined with `let`, followed by a variable name and a value. +Defined with `val`, followed by a variable name and a value. ```thp -let surname = "Doe" -let year_of_birth = 1984 +val surname = "Doe" +val year_of_birth = 1984 ``` ### Datatype annotation -Written after the `let` keyword but before the variable name. +Written after the `val` keyword but before the variable name. ```thp -let String surname = "Doe" -let Int year_of_birth = 1984 +val String surname = "Doe" +val Int year_of_birth = 1984 ``` -When annotating an immutable variable the `let` keyword is optional +When annotating an immutable variable the `val` keyword is optional ```thp // Equivalent to the previous code @@ -34,28 +34,28 @@ This means that if a variable has only a datatype, it is immutable. ## Mutable variables -Defined with `let mut`, followed by a variable name and a value. +Defined with `var`, followed by a variable name and a value. ```thp -let mut name = "John" -let mut age = 32 +var name = "John" +var age = 32 ``` ### Datatype annotation -Written after the `let mut` keywords but before the variable name. +Written after the `var` keywords but before the variable name. ```thp -let mut String name = "John" -let mut Int age = 32 +var String name = "John" +var Int age = 32 ``` -When annotating a mutable variable the keyword `let` is optional. `mut` is still **required**. +When annotating a mutable variable the keyword `var` is still **required**. ```thp // Equivalent to the previous code -mut String name = "John" -mut Int age = 32 +var String name = "John" +var Int age = 32 ``` diff --git a/md/learn/classes/definition.md b/md/learn/classes/definition.md index 24ea16b..db5f896 100644 --- a/md/learn/classes/definition.md +++ b/md/learn/classes/definition.md @@ -27,10 +27,10 @@ let instance = SimpleClass() class SimpleClass { // Properties are private by default - mut String? name = ... + var String? name = ... // Made public with `pub` - pub mut String? surname = ... + pub var String? surname = ... // Methods are private by default fun display_name() @@ -58,10 +58,9 @@ Kotlin style ```thp class Cat( - // If a parameter has pub, protected or private they are promoted to properties - private String name, - pub mut Int lives = 9, - protected String surname = "Doe", + var String name, + var Int lives = 9, + val String surname = "Doe", ) { pub fun get_name() -> String @@ -107,7 +106,7 @@ class Dog(pub String name) Kotlin style ```thp -class Animal(pub String name) +class Animal(var String name) { pub fun say_name() { @@ -120,6 +119,35 @@ class Cat(String name, Int lives) -> Animal(name) Cat("Michi", 9).say_name() ``` +## Mutable methods + +By default methods cannot mutate the state of the object. + +```thp +class Animal(var String name) +{ + pub fun set_name(String new_name) + { + $name = new_name // Error: Cannot mutate $ + } +} +``` + +To do so the method must be annotated. The caller must also +declare a mutable variable. + +```thp +class Animal(var String name) +{ + pub mut fun set_name(String new_name) + { + $name = new_name // Ok + } +} + +var michi = Animal("Michifu") +michi.set_name("Garfield") +``` diff --git a/md/learn/classes/magic.md b/md/learn/classes/magic.md index cc0b331..0d3e5cd 100644 --- a/md/learn/classes/magic.md +++ b/md/learn/classes/magic.md @@ -16,10 +16,9 @@ class Cat ```thp -let option = Some("GAAA") -let Some(value) = option - -let colors = Array("red", "green", "blue") -let Array() +val option = Some("GAAA") +val Some(value) = option +val colors = Array("red", "green", "blue") +val Array() ``` diff --git a/md/learn/collections/arrays.md b/md/learn/collections/arrays.md index e2d15f4..b23c506 100644 --- a/md/learn/collections/arrays.md +++ b/md/learn/collections/arrays.md @@ -5,13 +5,13 @@ Use square brackets as usual. ## Usage ```thp -let fruits = ["apple", "banana", "cherry"] -let apple = fruits[0] +val fruits = ["apple", "banana", "cherry"] +val apple = fruits[0] print(apple) -let mut numbers = [0, 1, 2, 3] +val mut numbers = [0, 1, 2, 3] numbers[3] = 5 diff --git a/md/learn/collections/maps.md b/md/learn/collections/maps.md index fd3f81f..661725c 100644 --- a/md/learn/collections/maps.md +++ b/md/learn/collections/maps.md @@ -6,7 +6,7 @@ Also known as Associative Arrays ## Usage without a declaration ```thp -let mut person = Obj { +val mut person = Obj { name: "John", surname: "Doe", age: 33, @@ -31,7 +31,7 @@ obj Person = { } -let john_doe = Person { +val john_doe = Person { name: "John", surname: "Doe", age: 33, diff --git a/md/learn/collections/sets.md b/md/learn/collections/sets.md index 75932f1..357823a 100644 --- a/md/learn/collections/sets.md +++ b/md/learn/collections/sets.md @@ -3,7 +3,7 @@ ```thp // Set[Int] -let ages = Set(30, 31, 33, 35) +val ages = Set(30, 31, 33, 35) for age in ages { print("{age}") diff --git a/md/learn/collections/tuples.md b/md/learn/collections/tuples.md index cd53916..3db7b71 100644 --- a/md/learn/collections/tuples.md +++ b/md/learn/collections/tuples.md @@ -6,9 +6,9 @@ calls (`()`). ## Definition ```thp -let person = #("John", "Doe", 32) +val person = #("John", "Doe", 32) -let #(name, surname, age) = person +val #(name, surname, age) = person ``` diff --git a/md/learn/flow-control/conditionals.md b/md/learn/flow-control/conditionals.md index 9a69c41..0846b95 100644 --- a/md/learn/flow-control/conditionals.md +++ b/md/learn/flow-control/conditionals.md @@ -22,7 +22,7 @@ else } -let result = if condition { value1 } else { value2 } +val result = if condition { value1 } else { value2 } ``` @@ -40,7 +40,7 @@ if variable is Datatype ## If variable is of enum ```thp -let user_id = POST::get("user_id") +val user_id = POST::get("user_id") if Some(user_id) = user_id { diff --git a/md/learn/flow-control/loops.md b/md/learn/flow-control/loops.md index 29bd095..75e420d 100644 --- a/md/learn/flow-control/loops.md +++ b/md/learn/flow-control/loops.md @@ -5,7 +5,7 @@ Braces are required. ```thp -let numbers = [0, 1, 2, 3] +val numbers = [0, 1, 2, 3] for number in numbers { @@ -19,7 +19,7 @@ for #(index, number) in numbers.entries() ``` ```thp -let dict = Obj { +val dict = Obj { apple: 10, banana: 7, cherries: 3, @@ -45,8 +45,8 @@ for value in collection ## While loop ```thp -let colors = ["red", "green", "blue"] -let mut index = 0 +val colors = ["red", "green", "blue"] +val mut index = 0 while index < colors.size() { diff --git a/md/learn/flow-control/match.md b/md/learn/flow-control/match.md index a5858b3..78d5c82 100644 --- a/md/learn/flow-control/match.md +++ b/md/learn/flow-control/match.md @@ -5,7 +5,7 @@ Braces are **required**. ```thp -let user_id = POST::get("user_id") +val user_id = POST::get("user_id") match user_id diff --git a/md/learn/functions/declaration.md b/md/learn/functions/declaration.md index 3e516c7..66ce732 100644 --- a/md/learn/functions/declaration.md +++ b/md/learn/functions/declaration.md @@ -21,7 +21,7 @@ fun get_random_number() -> Int Random::get(0, 35_222) } -let number = get_random_number() +val number = get_random_number() ``` ## With parameters and return type @@ -32,7 +32,7 @@ fun get_secure_random_number(Int min, Int max) -> Int Random::get_secure(min, max) } -let number = get_secure_random_number(0, 65535) +val number = get_secure_random_number(0, 65535) ``` @@ -44,10 +44,10 @@ fun get_first_item[T](Array[T] array) -> T array[0] } -let first = get_first_item[Int](numbers) +val first = get_first_item[Int](numbers) // The type annotation is optional if the compiler can infer the type -let first = get_first_item(numbers) +val first = get_first_item(numbers) ``` diff --git a/md/learn/functions/higher-order.md b/md/learn/functions/higher-order.md index 2bc3040..4ae4bd4 100644 --- a/md/learn/functions/higher-order.md +++ b/md/learn/functions/higher-order.md @@ -23,8 +23,8 @@ fun generate_generator() -> () -> Int } -let generator = generate_generator() // A function -let value = generate_generator()() // An Int +val generator = generate_generator() // A function +val value = generate_generator()() // An Int ``` diff --git a/md/learn/functions/lambdas.md b/md/learn/functions/lambdas.md index 921cae1..cd983db 100644 --- a/md/learn/functions/lambdas.md +++ b/md/learn/functions/lambdas.md @@ -22,9 +22,9 @@ By default closures **always** capture variables as **references**. ```thp -let mut x = 20 +val mut x = 20 -let f = fun() { +val f = fun() { print(x) } @@ -44,9 +44,9 @@ fun(parameters) clone(variables) { ``` ```thp -let mut x = 20 +val mut x = 20 -let f = fun() clone(x) { +val f = fun() clone(x) { print(x) } diff --git a/md/learn/functions/parameters.md b/md/learn/functions/parameters.md index fb607e4..4a802ab 100644 --- a/md/learn/functions/parameters.md +++ b/md/learn/functions/parameters.md @@ -10,16 +10,18 @@ fun add_25(Array[Int] numbers) { ``` When using a regular type as a parameter, only it's immutable -properties can be used inside the function +properties can be used ```thp fun count(Array[Int] numbers) -> Int { - let items_count = numbers.size() // Ok, `size` is pure + val items_count = numbers.size() // Ok, `size` is pure items_count } ``` +To use immutable properties you must use a mutable reference. + ## Mutable reference @@ -36,7 +38,7 @@ data **can** be mutated. The caller *must* also use `mut`. ```thp -let numbers = Array(0, 1, 2, 3) +val numbers = Array(0, 1, 2, 3) push_25(mut numbers) // Pass `numbers` as reference. @@ -58,7 +60,7 @@ of the parameter (CoW). The original data will **not** be mutated. ```thp -let numbers = Array(1, 2, 3, 4) +val numbers = Array(1, 2, 3, 4) add_25(clone numbers) // Pass `numbers` as clone. diff --git a/md/learn/index.md b/md/learn/index.md index 4b548ac..2bf17a6 100644 --- a/md/learn/index.md +++ b/md/learn/index.md @@ -55,7 +55,7 @@ These are **not** aspects that THP looks to solve or implement. $has_key = str_contains($haystack, 'needle'); // THP -let has_key = haystack.contains("needle") +val has_key = haystack.contains("needle") ``` - Explicit variable declaration @@ -83,7 +83,7 @@ Obj { ``` - Tuples, Arrays, Sets, Maps are clearly different -- JS-like object syntax +- JSON-like object syntax --- @@ -93,7 +93,7 @@ $cat = new Cat("Michifu", 7); $cat->meow(); // THP -let cat = Cat("Michifu", 7) +val cat = Cat("Michifu", 7) cat.meow(); ``` @@ -133,7 +133,7 @@ For example: ```thp // This expression -let greeting = +val greeting = match get_person() | Some(person) if person.age > 18 { @@ -176,7 +176,7 @@ enum IpAddress { V6(String), } -let ip_1 = IpAddress::V4("255.255.0.0") +val ip_1 = IpAddress::V4("255.255.0.0") // Would possibly compile to: diff --git a/static/index.html b/static/index.html index 0a97362..e4db9f9 100644 --- a/static/index.html +++ b/static/index.html @@ -13,7 +13,7 @@
Syntax, stdlib and types for PHP
+
+ Written in Rust