Skip to content

Instantly share code, notes, and snippets.

@wooosh
Created February 10, 2020 16:11
Show Gist options
  • Save wooosh/5f381870d028894e3b6f65853efb7f34 to your computer and use it in GitHub Desktop.
Save wooosh/5f381870d028894e3b6f65853efb7f34 to your computer and use it in GitHub Desktop.
blang example code
typedef ScarfDesign struct {
Pattern []byte
Length int
}
// If main throws an error, the return code will be set accordingly
fn main() {
// Try will use the default error message if catch is not used
{pattern, length} := try ReadDesign()
// Print scarf design
// Generator function
while range(length) {
try Print(pattern)
}
}
fn ReadDesign() ScarfDesign {
// standard library calls are not decided yet
try {
Print("Scarf Pattern: ")
// TODO: consider adding a way to automatically control copy buffer/new buffer as caller
// Read has multiple function signatures
pattern := Read()
Print("Scarf Length: ")
var buf []byte
read(&buf)
length := try int(buf)
return ScarfDesign{pattern, length}
} catch e {
case e.Type {
CastError {
error("Cannot parse intger:", err)
}
// IOError matches all errors related to IO
IOError {
error("Cannot access stdin/stdout", err)
}
default {
panic(e)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment