You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are many ways to contribute to nature: reporting BUGs, sharing ideas, participating in community discussions, coding, improving documentation, establishing standards, contributing resources, donations, and more.
type numbert = gen i8|i16|i32|i64|u8|u16|u32|u64|f32|f64
159
-
160
-
fn sum(numbert a, numbert b):numbert {
161
-
return a + b
162
-
}
163
-
fn cmp(numbert a, numbert b):bool {
164
-
return a > b
165
-
}
166
-
167
-
// type param
168
-
type box<t> = struct {
169
-
t width
170
-
t length
171
-
var area = fn(self s):t {
172
-
return s.width * s.length
173
-
}
174
-
}
175
-
176
-
fn run() {
177
-
var b = box<i8> {
178
-
width = 5,
179
-
length = 10
180
-
}
181
-
println('selfarea=', b.area())
182
-
}
183
-
184
-
```
185
-
186
-
Union Types:
187
-
188
-
```rust
189
-
type nullable<t> = t|null
190
-
191
-
nullable<i8> foo = 24
192
-
if foo is null {
193
-
// logic...
194
-
return
195
-
}
196
-
197
-
// x println(foo + 12), foo is a union type, cannot use binary
198
-
199
-
let foo as i8
200
-
println(foo + 12)
201
-
```
202
-
203
-
Function Tags:
204
-
205
-
```java
206
-
@local @retry=5
207
-
@test 24, 10 -> 4
208
-
@test -5, 10 -> -5
209
-
fn rem(int dividend, int divisor):int {
210
-
if divisor == 0 {
211
-
throw 'divisorcannotbezero'
212
-
}
213
-
return dividend % divisor
214
-
}
215
-
216
-
@global @post increase_views
217
-
fn read_blog():int {
218
-
// logic ...
219
-
}
220
-
221
-
@comment Based on label prompt + test for automatic code generation testing
222
-
@prompt sum up a and b
223
-
@test 12, 13 -> 25
224
-
@test -5, 10 -> 5
225
-
fn sum(int a, int b):int {}
226
-
```
227
-
228
-
HTTP Server:
229
-
230
-
```js
231
-
import http
232
-
import http.router
233
-
import http.resp
234
-
235
-
var app = http.server()
236
-
237
-
router.get('/', fn(ctx):resp {
238
-
return resp.string('helloworld')
239
-
})
240
-
241
-
app.use(router).listen('127.0.0.1', 8000)
242
-
```
243
-
244
-
For more coding examples 👉 [cases](https://github.com/nature-lang/nature/tree/master/tests/blackbox/cases)
245
-
246
-
## 📌 FAQ
247
-
248
-
1.Does nature use type prefix or suffix?
249
-
250
-
Nature consistently uses type prefixing, including the return type of functions. A primitive design example:
251
-
252
-
`fn sum(int a, int b):int c` shows that the function return type also uses type prefixing. Omitting all idents can lead to the function type declaration `fn(int,int):int`. Typically, the return value'sidentalsoneedstobeomitted, resultingintheformalfunctiondeclaration `fnsum(inta, intb):int {}`.
0 commit comments