@@ -18,20 +18,17 @@ io = import("std/io")
1818strings = import (" std/strings" )
1919
2020fn main() {
21- list = [" Not" , " Yet" , " Yellow" , " World" , " But" , " Almost" ]
21+ message =
22+ " hello world"
23+ | > strings.upper()
24+ | > append(" !" )
2225
23- text = list
24- | > slice (2 , len (list ) - 2 ) # Slice the list to get rid of extra words.
25- | > strings.join(" " ) # Create a string by joining words from the list.
26- | > strings.replace(" Yellow" , " Hello" ) # Fix the typo (Yellow > Hello).
27- | > append(" !" ) # Finally append !, although the + operator also works.
28-
29- io.println(text)
26+ io.println(message)
3027}
3128```
3229
3330```
34- Hello World !
31+ HELLO WORLD !
3532```
3633
3734## Philosophy
@@ -42,20 +39,17 @@ The core guiding principle is minimalism, but without making it boringly simple.
4239
4340## Features
4441
45- * Minimalist but ergonomic and consistent syntax
46- * Lists (immutable Python-like lists)
47- * Records (immutable Javascript-like objects)
48- * First-class functions
49- * Lambdas
50- * Closures
51- * Function pipe operator
42+ * Minimalist, ergonomic and consistent syntax
43+ * Immutable Python-like lists
44+ * Immutable JavaScript-like records
45+ * First-class functions, lambdas and closures
46+ * Function pipe operator (` |> ` )
5247* Easy collection iteration
53- * Functions that might fail return { "error": Bool, "value": Value }
54- * One single way to do things
55- * Rich built-in std library
56- * Balanced imperative/functional style
57- * Ideal for new and experienced programmers
58- * Implemented in Rust taking advantage of its ownership model (no GC)
48+ * Lightweight error propagation with ` ?= `
49+ * Rich standard library
50+ * Balanced imperative and functional style
51+ * Friendly to both new and experienced programmers
52+ * Implemented in Rust without a garbage collector
5953
6054## Examples
6155
@@ -128,57 +122,14 @@ Output:
128122
129123### Example 3
130124
131- ``` python
132- conv = import (" std/conv" )
133- io = import (" std/io" )
134-
135- # Define a function that "updates" a user (records are immutable).
136- fn birthday(user) {
137- return {
138- name: user.name,
139- age: user.age + 1
140- }
141- }
142-
143- # Create a record.
144- user = {
145- name: " Vasco" ,
146- age: 44
147- }
148-
149- io.println(" User:" )
150- io.println(" Name: " + user.name)
151- io.println(" Age: " + conv.string(user.age))
152-
153- # Create a new updated record.
154- updated = birthday(user)
155-
156- io.println()
157- io.println(" After birthday:" )
158- io.println(" Name: " + updated.name)
159- io.println(" Age: " + conv.string(updated.age))
160- ```
161-
162- ```
163- User:
164- Name: Vasco
165- Age: 44
166-
167- After birthday:
168- Name: Vasco
169- Age: 45
170- ```
171-
172- ### Example 4
173-
174125``` python
175126env = import (" std/env" )
176127http = import (" std/http" )
177128io = import (" std/io" )
178129strings = import (" std/strings" )
179130
180131fn get_weather(location) {
181- # Get may fail so it returns { "error": Bool, "value": Value }.
132+ # Get may fail so it returns { "error": Bool, "value": Any }.
182133 # The last expression is returned even without the return keyword.
183134 # A function returns an expression that evaluates to a value and is returned.
184135 http.get(" https://wttr.in/" + location + " ?format=3" )
@@ -200,7 +151,7 @@ fn main() {
200151
201152 result = get_weather(location)
202153
203- # By convention functions that might fail return { "error": Bool, "value": Value }.
154+ # By convention functions that might fail return { "error": Bool, "value": Any }.
204155 # Checking this record for an error is a common pattern in gluonscript.
205156 # If error is true, value shows its message as a string.
206157 # Otherwise if error is false, value shows whatever value the function returns.
@@ -216,4 +167,41 @@ fn main() {
216167```
217168Output:
218169lisbon: 🌦 +14°C
170+ ```
171+
172+ ### Example 4
173+
174+ ``` python
175+ http = import (" std/http" )
176+ io = import (" std/io" )
177+ json = import (" std/json" )
178+
179+ fn fetch(url) {
180+ # The ?= operator (error propagation) is similar to Rust's ? operator.
181+ # It makes it easy to handle { "error": Bool, "value": Any } return types.
182+ # If the value on the right is an error, it propagates the error to the caller.
183+ # Otherwise it unwraps value and assigns it to the variable on the left.
184+
185+ raw_json ? = http.get(url) # Get raw json from server or propagate error.
186+ parsed_json ? = json.parse(raw_json) # Parse JSON into a record or propagate error.
187+
188+ # Functions using the ?= operator should return { "error": Bool, "value": Any }.
189+ # This record is similar to Rust's Result type, but simpler and without generics.
190+ { error: false, value: parsed_json }
191+ }
192+
193+ fn main() {
194+ content = fetch(" https://catfact.ninja/fact" )
195+
196+ if content.error {
197+ io.println(" There was an error: " + content.value)
198+ return 1
199+ }
200+
201+ io.println(content.value.fact)
202+ }
203+ ```
204+
205+ ```
206+ A cat can jump 5 times as high as it is tall.
219207```
0 commit comments