this post was submitted on 14 Feb 2024
527 points (96.8% liked)

Programmer Humor

19623 readers
79 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
527
??? (lemm.ee)
submitted 9 months ago* (last edited 6 months ago) by [email protected] to c/[email protected]
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 6 points 9 months ago (1 children)
[–] [email protected] 1 points 9 months ago* (last edited 9 months ago) (2 children)

Thank you very much, I'm definitely going to take this for a spin! Can I ask if you or someone you know uses this? I'm curious what the experience is like and if theres any downfalls.

[–] [email protected] 2 points 9 months ago* (last edited 9 months ago)

mostly the Result type. MustGet where you'd except a panic OrElse to pass a fallback value (can be a function with return value of the same type, as the inner function, but without an error). Useful in e.g. more complex constructors where some fields might not be readily available. Either can for instance be useful to have arbitrary type unions in structs. I haven't used Option that much but seems similar to Rust's.

[–] [email protected] 2 points 9 months ago* (last edited 9 months ago)

A simple example:


func GetConfig(path string) mo.Result[*Config] {
return mo.Try(func (*Config, error) {
// logic to get the config
})
}

conf := GetConfig.OrElse(&DefaultConfig)

While it might not make much sense for a function you use just once, it can get actually pretty useful to simplify error handling like this for something you use more often.