Menu

Goose Functions: One Return, Two Returns, a Default
📰

Goose Functions: One Return, Two Returns, a Default

DEV Community: docker·flisk·about 4 hours ago
#ZYBENCJY
#devtodocker#devto#dev#fullscreen#print
Reading 0:00
15s threshold

Part 2 of Goose, then any language on Flisk. Still no Docker. Run each file with goose file.goose .

Functions are free functions. There is no impl and no method table. xs.total() is exactly total(xs) .

One return

total.goose :

fn total(xs: i64[:]) -> i64 { var t = 0; for x in xs { t += x; } return t; }

fn main() { let nums: i64[] =;[10][12][20] print(total(nums)); print(nums.total()); }

Enter fullscreen mode

Exit fullscreen mode

i64[:] is a slice: a reference plus a count. The function does not copy the elements. let cannot be reassigned. var can. A block's value is its last expression, so a short function can omit return :

fn limit(x: i64, lo: i64 = 0, hi: i64 = 100) -> i64 { if x < lo { lo } else if x > hi { hi } else { x } }

Enter fullscreen mode

Exit fullscreen mode

A default is evaluated at each call that leaves it out. It may name globals and other functions.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.