Changes in the syntax
This commit is contained in:
parent
49a04fa5ac
commit
1e63e799ce
@ -11,7 +11,7 @@ val apple = fruits[0]
|
|||||||
print(apple)
|
print(apple)
|
||||||
|
|
||||||
|
|
||||||
val mut numbers = [0, 1, 2, 3]
|
var numbers = [0, 1, 2, 3]
|
||||||
|
|
||||||
numbers[3] = 5
|
numbers[3] = 5
|
||||||
|
|
||||||
|
@ -1,43 +1,94 @@
|
|||||||
# Maps
|
# Maps
|
||||||
|
|
||||||
Also known as Associative Arrays
|
Also known as Associative Arrays, or Objects in other languages.
|
||||||
|
|
||||||
|
All maps must have a definition, which define their fields and datatypes.
|
||||||
|
There can also be anonymous maps, which may contain any key.
|
||||||
|
|
||||||
## Usage without a declaration
|
## Named Maps
|
||||||
|
|
||||||
```thp
|
```thp
|
||||||
val mut person = Obj {
|
// Here we define a map, called Person
|
||||||
name: "John",
|
map Person {
|
||||||
surname: "Doe",
|
|
||||||
age: 33,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
print("Hello {person.name}")
|
|
||||||
|
|
||||||
person.age += 1
|
|
||||||
|
|
||||||
print("You just turned {person.age}")
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Usage with a declaration
|
|
||||||
|
|
||||||
```thp
|
|
||||||
obj Person = {
|
|
||||||
String name,
|
String name,
|
||||||
String surname,
|
String surname,
|
||||||
Int age,
|
Int age,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Here we declare an instance of a Person.
|
||||||
val john_doe = Person {
|
val john_doe = Person {
|
||||||
name: "John",
|
name: "John",
|
||||||
surname: "Doe",
|
surname: "Doe",
|
||||||
age: 33,
|
age: 33,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the compiler can infer the type of a Map,
|
||||||
|
// we can omit its type
|
||||||
|
var Person mary_jane = .{
|
||||||
|
name: "Mary",
|
||||||
|
surname: "Jane",
|
||||||
|
age: 27,
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To access the fields of a map we use square braces `[]`.
|
||||||
|
|
||||||
|
```thp
|
||||||
|
mary_jane[age] += 1
|
||||||
|
print(mary_jane[name]) // Mary
|
||||||
|
```
|
||||||
|
|
||||||
|
## Anonymous maps
|
||||||
|
|
||||||
|
An anonymous map allows us to store and retrieve any key of any datatype.
|
||||||
|
They are declared as `Map`.
|
||||||
|
|
||||||
|
```thp
|
||||||
|
val car = Map {
|
||||||
|
brand: "Toyota",
|
||||||
|
model: "Corolla",
|
||||||
|
year: 2012,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Anonymous maps can also can have their type omitted.
|
||||||
|
|
||||||
|
```thp
|
||||||
|
var car = .{
|
||||||
|
brand: "Toyota",
|
||||||
|
model: "Corolla",
|
||||||
|
year: 2012,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If the compiler encounters a map without a type (that is, `.{}`)
|
||||||
|
and doesn't expect a specific type, it will assume it is an
|
||||||
|
anonymous map.
|
||||||
|
|
||||||
|
We can freely assign fields to an anonymous map:
|
||||||
|
|
||||||
|
```thp
|
||||||
|
// Modify an existing field
|
||||||
|
car[year] = 2015
|
||||||
|
// Create a new field
|
||||||
|
car[status] = "used"
|
||||||
|
```
|
||||||
|
|
||||||
|
However, if we try to access a field of an anonymous map we'll get an error.
|
||||||
|
|
||||||
|
```thp
|
||||||
|
print(car[status]) // Error: Can't access a field of an anonymous map
|
||||||
|
```
|
||||||
|
|
||||||
|
Instead, we should use the `get` function of the map, which expects a
|
||||||
|
datatype and returns an Option
|
||||||
|
|
||||||
|
```thp
|
||||||
|
// | this function
|
||||||
|
String? car_status = car.get[String]("status")
|
||||||
|
```
|
||||||
|
|
||||||
|
The `get` function will check that a key `"status"` exists in the map,
|
||||||
|
and that it has the correct datatype. If either the key doesn't exist
|
||||||
|
or it has a different datatype, it will return `None`.
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
# Tuples
|
# Tuples
|
||||||
|
|
||||||
Uses `#()` just to avoid confusion with function
|
Uses `#()` just to avoid confusion with function calls and grouping (`()`).
|
||||||
calls (`()`).
|
|
||||||
|
|
||||||
## Definition
|
## Definition
|
||||||
|
|
||||||
|
@ -9,31 +9,38 @@ val user_id = POST::get("user_id")
|
|||||||
|
|
||||||
|
|
||||||
match user_id
|
match user_id
|
||||||
| Some(id) { print("user_id exists: {id}") }
|
case Some(id) { print("user_id exists: {id}") }
|
||||||
| None { print("user_id doesn't exist") }
|
case None { print("user_id doesn't exist") }
|
||||||
|
|
||||||
match user_id
|
match user_id
|
||||||
| Some(id)
|
case Some(id)
|
||||||
{
|
{
|
||||||
print("user_id exists: {id}")
|
print("user_id exists: {id}")
|
||||||
}
|
}
|
||||||
| None
|
case None
|
||||||
{
|
{
|
||||||
print("user_id doesn't exist")
|
print("user_id doesn't exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
match user_id
|
match user_id
|
||||||
| Some(id) if id > 0
|
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!")
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print("hello dear")
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,11 +59,22 @@ f() // 20
|
|||||||
|
|
||||||
## Lambdas
|
## Lambdas
|
||||||
|
|
||||||
|
Lambdas are a short form of anonymous functions. They are declared with `#{}`.
|
||||||
|
|
||||||
|
Inside the lambda you can access the parameters as `$1`, `$2`, etc.
|
||||||
|
|
||||||
|
Finally, lambdas be written outside of the parenthesis of function calls.
|
||||||
|
|
||||||
```thp
|
```thp
|
||||||
numbers.map {
|
numbers.map() #{
|
||||||
$0 * 2
|
$1 * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the above lambda is equivalent to:
|
||||||
|
|
||||||
|
numbers.map(fun(param1) {
|
||||||
|
$1 * 2
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
--code-theme-bg-color: #ffffff;
|
--code-theme-bg-color: #ffffff;
|
||||||
--code-theme-bg-color_selection: #a4cdff;
|
--code-theme-bg-color_selection: #a4cdff;
|
||||||
--code-theme-color_selection: inherit;
|
--code-theme-color_selection: inherit;
|
||||||
|
--code-theme-border-color: #c2c2c2;
|
||||||
|
|
||||||
--code-theme-comment: #5d6c79;
|
--code-theme-comment: #5d6c79;
|
||||||
|
|
||||||
@ -92,7 +93,6 @@ pre[class*="language-"] ::selection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
:not(pre) > code {
|
:not(pre) > code {
|
||||||
/* border: solid 1px var(--border-color); */
|
|
||||||
background-color: var(--code-theme-bg-color);
|
background-color: var(--code-theme-bg-color);
|
||||||
padding: 0 0.25rem;
|
padding: 0 0.25rem;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
@ -103,6 +103,7 @@ pre[class*="language-"] {
|
|||||||
position: relative;
|
position: relative;
|
||||||
margin: 0.5em 0;
|
margin: 0.5em 0;
|
||||||
padding: 0.75em 0.75em;
|
padding: 0.75em 0.75em;
|
||||||
|
border: 1px solid var(--code-theme-border-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
.language-css > code,
|
.language-css > code,
|
||||||
|
@ -15,7 +15,7 @@ Prism.languages.thp = {
|
|||||||
pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
pattern: /(["])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
|
||||||
greedy: true,
|
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|val|var|mut|clone)\b/,
|
"keyword": /\b(?:case|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|map|override|open|init|val|var|mut|clone)\b/,
|
||||||
"number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
|
"number": /\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,
|
||||||
"operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/,
|
"operator": /[<>]=?|[!=]=?=?|--?|\$|\+\+?|&&?|\|\|?|[?*/~^%]/,
|
||||||
"punctuation": /[{}[\];(),.]/,
|
"punctuation": /[{}[\];(),.]/,
|
||||||
|
Loading…
Reference in New Issue
Block a user