Replace exists-keyword with ?-operator

This commit is contained in:
Teascade 2017-08-27 00:07:20 +00:00
parent e7889da465
commit 4c7a23867e

View File

@ -105,6 +105,9 @@ These are the operators ofthen called as "conditions" and most commonly used in
- `!` Not unary operator. Negates the value associated with it. - `!` Not unary operator. Negates the value associated with it.
- `!true` returns false - `!true` returns false
- `!(true ^ true)` returns true - `!(true ^ true)` returns true
- `?` Optional-exists unary operator. Checks weather the operator-wrapped value before the operator is empty or not.
- `empty?` returns false. See [`empty`](#empty)-keyword
- `optional?` returns true, if optional contains a value.
### Arithmetic operators ### Arithmetic operators
Arithmetic operators are operators used to do math calculations such as addition or multiplication. any integer or float -based types can be used together. Arithmetic operators are operators used to do math calculations such as addition or multiplication. any integer or float -based types can be used together.
@ -125,7 +128,6 @@ Arithmetic operators are operators used to do math calculations such as addition
- `2 % 3` returns 2 - `2 % 3` returns 2
- `6 % 2` returns 0 - `6 % 2` returns 0
### Assignment operators ### Assignment operators
Assignment operators are a special kind of operator used assign values to variables. Assignment operators are a special kind of operator used assign values to variables.
The most basic type of assignment operator being of course `=`. The most basic type of assignment operator being of course `=`.
@ -195,7 +197,6 @@ The following keywords are specified to execute an action.
- [`continue`](#continue) skips the rest of the loop's body for a new iteration. - [`continue`](#continue) skips the rest of the loop's body for a new iteration.
- [`unwrap`](#unwrap) unwraps an optional-type variable. - [`unwrap`](#unwrap) unwraps an optional-type variable.
- [`some`](#some) wraps a variable into an optional. - [`some`](#some) wraps a variable into an optional.
- [`exists`](#exists) tests weather an optional is not empty.
- [`empty`](#empty) value to set in an optional variable. - [`empty`](#empty) value to set in an optional variable.
- [`as`](#as) casts the leftside value into the type on the rightside of this keyword. - [`as`](#as) casts the leftside value into the type on the rightside of this keyword.
- [All operators](#operators) are also somewhat considered as keywords. - [All operators](#operators) are also somewhat considered as keywords.
@ -359,27 +360,11 @@ let optional: i32? = some number;
- Some must be followed by a variable. The variable is then wrapped into an optional and the optional is returned. - Some must be followed by a variable. The variable is then wrapped into an optional and the optional is returned.
- There must be a space between the `some` and the variable. - There must be a space between the `some` and the variable.
#### `exists`
Exists is a keyword that is used to check weather an optional variable contains a value or not.
```
let optional: i32? = something();
if (optional exists) {
// optional contains a value
} else {
// optional is empty.
}
let op_exists: boolean = optional exists;
```
- Unlike `unwrap` and `some`, `exists` must follow the optional value. Exists-keyword then returns the result as a boolean.
- There must be a space between the optional and `exists`
- If `exists` is after a non-optional variable, an exeption occurs.
#### `empty` #### `empty`
Empty is the keyword used to set an optional as empty (to not contain a value). Empty is the keyword used to set an optional as empty (to not contain a value).
``` ```
let optional: i32? = empty; let optional: i32? = empty;
if (optional exists) { if (optional?) {
// Never executed // Never executed
} else { } else {
// Executed, since optional is empty // Executed, since optional is empty
@ -395,7 +380,7 @@ As is the keyword used when you need to cast a variable to another. It will retu
let long: i64 = 5; let long: i64 = 5;
let int_opt: i32? = long as i32; let int_opt: i32? = long as i32;
let int = 0; let int = 0;
if (int_opt exists) { if (int_opt?) {
int = unwrap int_opt; int = unwrap int_opt;
} else { } else {
// Cast failed // Cast failed