Use val and var

master
Araozu 2024-02-20 05:17:21 -05:00
parent 31cb29fe85
commit 46f162620b
21 changed files with 108 additions and 73 deletions

View File

@ -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)

View File

@ -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
```

View File

@ -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")
```

View File

@ -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()
```

View File

@ -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

View File

@ -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,

View File

@ -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}")

View File

@ -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
```

View File

@ -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
{

View File

@ -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()
{

View File

@ -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

View File

@ -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)
```

View File

@ -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
```

View File

@ -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)
}

View File

@ -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.

View File

@ -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:

View File

@ -13,7 +13,7 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&family=Fugaz+One&family=Inconsolata&family=Inter&display=swap"
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&display=swap"
rel="stylesheet">
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap"
@ -40,6 +40,8 @@
</h1>
<p class="font-display text-c-text opacity-60 text-xl pt-4">
Syntax, stdlib and types for PHP
<br>
Written in Rust
</p>
</div>
@ -86,7 +88,11 @@
Learn
</a>
<a class="inline-block font-display text-lg border-2 border-[#5BCEFA] py-3 px-8 mx-6 rounded" href="/install/">
<a
class="inline-block font-display text-lg border-2 border-sky-400 py-3 px-8 mx-6 rounded
transition-colors hover:text-black hover:bg-sky-400"
href="/install/"
>
Install
</a>
</div>

View File

@ -15,7 +15,7 @@ Prism.languages.thp = {
pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
greedy: true,
},
"keyword": /\b(?:static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|obj|override|open|init|let|mut|clone)\b/,
"keyword": /\b(?:static|const|enum|loop|use|break|catch|continue|do|else|finally|for|fun|if|in|fn|nil|return|throw|try|while|type|match|with|of|abstract|class|interface|private|pub|obj|override|open|init|val|var|mut|clone)\b/,
"number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
"operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/,
"punctuation": /[{}[\];(),.]/,

View File

@ -14,14 +14,11 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&family=Fugaz+One&family=Inconsolata&family=Inter&display=swap"
href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@400;500;600;700;800;900&display=swap"
rel="stylesheet">
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Josefin+Sans:ital,wght@0,400;1,700&display=swap"
rel="stylesheet">
<style>
</style>
</head>
<body class="bg-c-bg text-c-text">
@ -44,7 +41,7 @@
<div class="pt-12 max-h-screen border-r border-r-[rgba(91,205,250,0.25)] overflow-x-scroll sticky top-0">
<nav class="rounded-md mt-2 p-4">
<h2 class="text-xl font-display pb-4">On this page</h2>
<h2 class="text-xl font-display pb-4 opacity-75">On this page</h2>
{{sidebar}}
</nav>

View File

@ -22,11 +22,14 @@ module.exports = {
"body": ["'Fira Sans'", "Inter", "sans-serif"],
},
},
corePlugins: {
container: false
},
plugins: [
function ({ addComponents }) {
addComponents({
'.container': {
maxWidth: '95%',
width: '98%',
'@screen sm': {
maxWidth: '640px',
},

View File

@ -17,7 +17,7 @@
@media (prefers-color-scheme: light) {
:root {
--c-bg: rgb(255, 247, 255);
--c-bg: rgb(255, 253, 255);
--c-text: #121212;
--c-purple: #374259;
--c-purple-light: #BA94D1;