v0.0.9: change how functions work
This commit is contained in:
27
spec.md
27
spec.md
@@ -10,6 +10,8 @@ Most important ones:
|
|||||||
- `List[T]`: (linked-) list of `T`
|
- `List[T]`: (linked-) list of `T`
|
||||||
- `Char`: unicode codepoint
|
- `Char`: unicode codepoint
|
||||||
- `Unit`: nothing
|
- `Unit`: nothing
|
||||||
|
- `i -> o`: function
|
||||||
|
- `template a, b: t`
|
||||||
|
|
||||||
Advanced (uncommon) types:
|
Advanced (uncommon) types:
|
||||||
- `Int8`, `Int16`, ...
|
- `Int8`, `Int16`, ...
|
||||||
@@ -18,23 +20,43 @@ Advanced (uncommon) types:
|
|||||||
|
|
||||||
## Functions
|
## Functions
|
||||||
```
|
```
|
||||||
|
# the type of msg is: List[Char] -> List[Char]
|
||||||
def msg(username: List[Char]) : List[Char] {
|
def msg(username: List[Char]) : List[Char] {
|
||||||
"Hello, " ++ username ++ "!" # last expr is returned
|
"Hello, " ++ username ++ "!" # last expr is returned
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# the type of main is: Unit -> Unit
|
||||||
def main() {
|
def main() {
|
||||||
print("hi")
|
print("hi")
|
||||||
}
|
}
|
||||||
main()
|
main()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Anonymus functions
|
||||||
|
```
|
||||||
|
The type of List.map is List[t] -> (t -> t) -> List[t]
|
||||||
|
List.map(li, x:Num -> x * 2)
|
||||||
|
```
|
||||||
|
|
||||||
## Simple, forward type-inference
|
## Simple, forward type-inference
|
||||||
```
|
```
|
||||||
def zero () : Flt32 {
|
def zero () : Flt32 {
|
||||||
3.1 # error: got Num, but expected F32
|
3.1 # error: got Num, but expected Flt32
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Partial function applications
|
||||||
|
```
|
||||||
|
# type of add is Num -> Num -> Num
|
||||||
|
def add(a: Num, b: Num) : Num {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
|
||||||
|
let a = add(1) # type of a is Num -> Num
|
||||||
|
let b = a(2) # type of b is Num
|
||||||
|
# b is 3
|
||||||
|
```
|
||||||
|
|
||||||
## Bindings
|
## Bindings
|
||||||
```
|
```
|
||||||
let name = "Max"
|
let name = "Max"
|
||||||
@@ -134,12 +156,15 @@ def add(a: Num, b: Num) -> _ {
|
|||||||
|
|
||||||
## templated generics
|
## templated generics
|
||||||
```
|
```
|
||||||
|
# Type of add is: template a, b: a -> b -> _
|
||||||
def [a,b] add(a: a, b: b) -> _ {
|
def [a,b] add(a: a, b: b) -> _ {
|
||||||
a + b
|
a + b
|
||||||
}
|
}
|
||||||
|
|
||||||
add(1,2)
|
add(1,2)
|
||||||
|
|
||||||
|
add(1)(2) # error: partial function application of templated functions not allowed
|
||||||
|
|
||||||
add(1,"2") # error: in template expansion of add[Num,List[Char]]: No definition for `Num + List[Char]`
|
add(1,"2") # error: in template expansion of add[Num,List[Char]]: No definition for `Num + List[Char]`
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user