Skip to content
Discussion options

You must be logged in to vote

In Odin, .? is an optional/union extraction operator with type inference.

v.? returns two values:
(value, ok)

Where:

  • value → extracted value
  • ok → bool indicating success

Example:

halve :: proc(n: int) -> Maybe(int) {
    if n % 2 != 0 do return nil
    return n / 2
}

half, ok := halve(2).?

Result

If value exists:

half = 1
ok   = true

If value is nil:

half = 0      // default value of int
ok   = false

If you're on Discord, feel free to ask here:
https://discord.gg/hHxthXp2UD

Even GingerBill sometimes replies.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hintron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants