-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.jul
63 lines (58 loc) · 1.68 KB
/
types.jul
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Typen sind Funktionen: Any => Boolean
# Typen beginnen mit Großbuchstaben.
# TODO TypParameter auch?
# mit : wird einer Variable ein Typ als Guard hinzugefügt
myVar: Text = §hallo§
# Wenn ein ungültiger Wert zur Comilezeit zugewiesen wird, wird ein Compilerfehler geliefert
# Wenn ein ungültiger Wert zur Laufzeit zugewiesen wird, wird stattdessen ein Error Object zugewiesen
myVar: Text = 4
# zur Laufzeit äquivalent zu:
myVar = (
type = TypeError
message = §4 can not be assigned to myVar because it is not of type Text§
)
# prüfe ob eine Variable vom Typ ist
myVarIsText = Text(myVar) # => true
# Objekt Type Literal:
Article = (
name: Text
price: Decimal
color: Text
)
# Ist äquivalent zu:
Article =
# Union Type:
Number = or(Int32 Int64 Integer Float Decimal Real0)
# Text und Number Literale können auch als Typ verwendet werden
myVar: §5§ = §5§
myVar: 4 = 3 # => Error
# Generische Funktion:
# TODO syntax überdenken
addNums = (num1: T num2: T) :> T where T: Number =>
(num1 num2) ?
(num1: Int32 num2: Int32) => addInt32(num1 num2)
(num1: Int64 num2: Int64) => addInt64(num1 num2)
...
# Generische Funktion als Funktion höherer Ordnung:
addNums = (T: Number) =>
(num1: T num2: T) :> T =>
(num1 num2) ?
(num1: Int32 num2: Int32) => addInt32(num1 num2)
(num1: Int64 num2: Int64) => addInt64(num1 num2)
...
# Generische Typen:
ParserResult(T: Text) = (
type: T
errors: List<Text>
)
# Generischer Typ als Funktion höherer Ordnung:
ParserResult = (TypeParam: Text) => (
type: TypeParam
errors: List(Text)
)
ParserResult = (TypeParam: Text) =>
x =>
and(TypeParam(x.type) List(Text)(x.errors))
# Typ mit Funktion definiert:
Even = num =>
equal(modulo(num 2) 0)