Skip to content
Discussion options

You must be logged in to vote

Your line will work if there exist err variable like in next program.

fn divide(a int, b int) ! int {
	if b == 0 {
		return error('denominator can\'t be zero')
	}
	return a/b
}

fn main() {
	q := divide(10, 0) or {
		assert false, 'divide error: ${err}'
		0
	}
	dump(q)
}

https://play.vlang.io/p/cb4fa0ac1d

code.v:10:NaN: fn main.main: assert false, 'divide error: $err'
      message: divide error: denominator can't be zero
V panic: Assertion failed...

The divide function returns a Result, that is, a possibility of a value or an error (when denominator is zero). By calling the divide function the compiler requires an or { ... } block where to go when the error is triggered, inside the block

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@toolateralus
Comment options

@toolateralus
Comment options

@jorgeluismireles
Comment options

Answer selected by toolateralus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants