Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit ec685aa

Browse files
authored
📝 add 2 new tutorials: 🙀 OMG there is no class in Golo (#556)
📝 add 2 new tutorials: OMG there is no class in Golo
1 parent 39072d4 commit ec685aa

9 files changed

+435
-1
lines changed

tutorials/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@
55
## Introduction
66

77
- [Build Golo within a VM](build-golo.md)
8-
- [Golo first steps](first-steps.md)
8+
- [Golo first steps](first-steps.md)
9+
- There is no class, but...
10+
- [Structures](structures.md)
11+
- [DynamicObjects](dynamicobjects.md)

tutorials/dynamicobjects.md

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# Lovely DynamicObjects 😍
2+
3+
_Dynamic objects can have values and methods being added and removed dynamically at runtime._
4+
5+
You can think of this like the `{}` (`object`) of JavaScript (btw, I'm crazy of JavaScript) or the `DynamicObject` class of `.Net`.
6+
7+
## Define a DynamicObject
8+
9+
> **01-dynamicobjects-fields.golo**
10+
```golo
11+
module hello.dynamic.objects
12+
13+
function main = |args| {
14+
15+
let jane = DynamicObject(): firstName("Jane"): lastName("Doe") # 1️⃣
16+
println(jane) # 2️⃣
17+
18+
let john = DynamicObject() # 3️⃣
19+
println(john) # 4️⃣
20+
john: firstName("John") # 5️⃣
21+
john: lastName("Doe") # 5️⃣
22+
println(john) # 6️⃣
23+
24+
println("Hello I'm " + jane: firstName() + " " + jane: lastName()) # 7️⃣
25+
26+
}
27+
```
28+
29+
> - 1️⃣ you can define a DynamicObject everywhere and its fields at the same time
30+
> - 2️⃣ it will print `DynamicObject{firstName=Jane, lastName=Doe}`
31+
> - 3️⃣ you can define an "empty" DynamicObject and add the fields later
32+
> - 4️⃣ it will print `DynamicObject{}`
33+
> - 5️⃣ add setters and values dynamically
34+
> - 6️⃣ it will print `DynamicObject{firstName=John, lastName=Doe}`
35+
> - 7️⃣ use of getters; and it will print `Hello I'm Jane Doe`
36+
37+
**To run it**, type this command: `golo golo --files 01-dynamicobjects-fields.golo`
38+
39+
## Add methods to a DynamicObject
40+
41+
Adding a method to a DynamicObject is like to add a closure to a field.
42+
43+
> **02-dynamicobjects-methods.golo**
44+
```golo
45+
module hello.dynamic.objects
46+
47+
function main = |args| {
48+
49+
let jane = DynamicObject()
50+
: firstName("Jane")
51+
: lastName("Doe")
52+
: hello(|this| { # 1️⃣
53+
println("Hello I'm " + this: firstName() + " " + this: lastName())
54+
})
55+
: message(|this, message| { # 2️⃣
56+
println(
57+
"I'm " +
58+
this: firstName() + " " +
59+
this: lastName() + ": " +
60+
message
61+
)
62+
})
63+
64+
jane: hello() # 3️⃣
65+
jane: message("👋 hello world 🌍") # 4️⃣
66+
67+
jane: about(|this| -> "I'm " + this: firstName()) # 5️⃣
68+
println(jane: about()) # 6️⃣
69+
}
70+
71+
```
72+
73+
> - 1️⃣ the name pf the method is the name of the field; and the value is a closure with a self reference to the object as the first parameter
74+
> - 2️⃣ a DynamicObject method can have several parameters (with always the first parameter as a reference to the current object)
75+
> - 3️⃣ it will print `Hello I'm Jane Doe`
76+
> - 4️⃣ it will print `I'm Jane Doe: 👋 hello world 🌍`
77+
> - 5️⃣ you can add a method dynamically when/where you want
78+
> - 6️⃣ it will print `I'm Jane`
79+
80+
**To run it**, type this command: `golo golo --files 02-dynamicobjects-methods.golo`
81+
82+
## Use a function as a constructor
83+
84+
If you need a constructor for you DynamicObject, it's easy! Use a function returning a DynamicObject:
85+
86+
> **03-dynamicobjects-constructors.golo**
87+
```golo
88+
module hello.dynamic.objects
89+
90+
function Human = |firstName, lastName| { # 1️⃣
91+
return DynamicObject()
92+
: firstName(firstName)
93+
: lastName(lastName)
94+
: hello(|this| {
95+
println("Hello I'm " + this: firstName() + " " + this: lastName())
96+
})
97+
: message(|this, message| {
98+
println(
99+
"I'm " +
100+
this: firstName() + " " +
101+
this: lastName() + ": " +
102+
message
103+
)
104+
})
105+
}
106+
107+
function main = |args| {
108+
109+
let jane = Human("Jane", "Doe") # 2️⃣
110+
111+
jane: hello() # 3️⃣
112+
jane: message("👋 hello world 🌍") # 4️⃣
113+
114+
}
115+
```
116+
117+
> - 1️⃣ define a function returning a DynamicObject
118+
> - 2️⃣ call the function
119+
> - 3️⃣ it will print `Hello I'm Jane Doe`
120+
> - 4️⃣ it will print `I'm Jane Doe: 👋 hello world 🌍`
121+
122+
**To run it**, type this command: `golo golo --files 03-dynamicobjects-constructors.golo`
123+
124+
ℹ️ more about dynamic objects: https://golo-lang.org/documentation/next/#_dynamic_objects
125+
126+
**This is the end of the "DynamicObjects' tutorial"**. You can retrieve the source code here: [resources/dynamicobjects](resources/dynamicobjects).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module hello.dynamic.objects
2+
3+
function main = |args| {
4+
5+
let jane = DynamicObject(): firstName("Jane"): lastName("Doe")
6+
println(jane)
7+
8+
let john = DynamicObject()
9+
println(john)
10+
john: firstName("John")
11+
john: lastName("Doe")
12+
println(john)
13+
14+
println("Hello I'm " + jane: firstName() + " " + jane: lastName())
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module hello.dynamic.objects
2+
3+
function main = |args| {
4+
5+
let jane = DynamicObject()
6+
: firstName("Jane")
7+
: lastName("Doe")
8+
: hello(|this| {
9+
println("Hello I'm " + this: firstName() + " " + this: lastName())
10+
})
11+
: message(|this, message| {
12+
println(
13+
"I'm " +
14+
this: firstName() + " " +
15+
this: lastName() + ": " +
16+
message
17+
)
18+
})
19+
20+
jane: hello()
21+
jane: message("👋 hello world 🌍")
22+
23+
jane: about(|this| -> "I'm " + this: firstName())
24+
println(jane: about())
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module hello.dynamic.objects
2+
3+
function Human = |firstName, lastName| {
4+
return DynamicObject()
5+
: firstName(firstName)
6+
: lastName(lastName)
7+
: hello(|this| {
8+
println("Hello I'm " + this: firstName() + " " + this: lastName())
9+
})
10+
: message(|this, message| {
11+
println(
12+
"I'm " +
13+
this: firstName() + " " +
14+
this: lastName() + ": " +
15+
message
16+
)
17+
})
18+
}
19+
20+
function main = |args| {
21+
22+
let jane = Human("Jane", "Doe")
23+
24+
jane: hello()
25+
jane: message("👋 hello world 🌍")
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module hello.structure
2+
3+
# you can define structures in external modules
4+
5+
struct Human = {
6+
firstName,
7+
lastName
8+
}
9+
10+
function main = |args| {
11+
12+
let jane = Human("Jane", "Doe")
13+
# or let jane = Human(): firstName("Jane"): lastName("Doe")
14+
println(jane)
15+
16+
let john = Human()
17+
println(john)
18+
john: firstName("John")
19+
john: lastName("Doe")
20+
println(john)
21+
22+
println("Hello I'm " + jane: firstName() + " " + jane: lastName())
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module hello.structure
2+
3+
# you can define structures in external modules
4+
5+
struct Human = {
6+
firstName,
7+
lastName
8+
}
9+
10+
augment Human {
11+
function sayHello = |this| {
12+
println("Hello I'm " + this: firstName() + " " + this: lastName())
13+
}
14+
15+
function saySomething = |self, something| {
16+
println(self: firstName() + " " + self: lastName() + " says:" + something)
17+
}
18+
}
19+
20+
function main = |args| {
21+
22+
let jane = Human("Jane", "Doe")
23+
let john = Human("John", "Doe")
24+
25+
jane: sayHello()
26+
john: saySomething("hey! What's up?")
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module hello.structure
2+
3+
# you can define structures in external modules
4+
5+
struct Dog = { name }
6+
struct Cat = { name }
7+
8+
augmentation runnable = {
9+
function run = |this| -> println(this: name() + " is running")
10+
function walk = |this| -> println(this: name() + " is walking")
11+
}
12+
13+
augmentation woofable = {
14+
function woof = |this| -> println(this: name() + ": woof woof")
15+
}
16+
17+
augmentation meowable = {
18+
function meow = |this| -> println(this: name() + ": meow meow")
19+
}
20+
21+
augment Dog with runnable, woofable
22+
augment Cat with runnable, meowable
23+
24+
function main = |args| {
25+
26+
let kitty = Cat("Kitty")
27+
kitty: run()
28+
kitty: walk()
29+
kitty: meow()
30+
31+
let wolf = Dog("Wolf")
32+
wolf: run()
33+
wolf: walk()
34+
wolf: woof()
35+
}

0 commit comments

Comments
 (0)