|
| 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). |
0 commit comments