v icon indicating copy to clipboard operation
v copied to clipboard

c error while there is a `or` on the return line

Open vincenzopalazzo opened this issue 2 years ago • 8 comments

While working on https://github.com/vlang/v/issues/15197 I found another wired bug while I run the following code

gitpod /workspace/v (macros/io.read) $ cat test.v 
fn unwrap_int() ?int {
	return 1
}

fn unwrap_function() !int {
	return unwrap_int() or {error("we are issing the return?")}
}

fn main() {
	x := unwrap_int() or {panic(err)}
	println(x)
}

vincenzopalazzo avatar Jul 25 '22 17:07 vincenzopalazzo

Shouldn't

fn unwrap_function() !int {
	return unwrap_int() or {error("we are issing the return?")}
}

be this, instead?

fn unwrap_function() !int {
	return unwrap_int() or {none}
}

JalonSolov avatar Jul 25 '22 17:07 JalonSolov

mh!

I think the return type is a result, so or a value (int) or a error, so I make the compiler happy by using the or {return error("...")}

vincenzopalazzo avatar Jul 25 '22 18:07 vincenzopalazzo

Well... ?int is optional return, meaning valid value or error. !int means result return, meaning value value or none. You have !int as the return type.

JalonSolov avatar Jul 25 '22 18:07 JalonSolov

?int: value of type int or none !int: value of type int or error

StunxFS avatar Jul 25 '22 18:07 StunxFS

Well... ?int is optional return, meaning valid value or error. !int means result return, meaning value value or none. You have !int as the return type.

I do not know, I just follow the RFC and he said the opposite for ?int

vincenzopalazzo avatar Jul 25 '22 18:07 vincenzopalazzo

@JalonSolov after the split none can only be used with ?Foo, error with !Foo.

medvednikov avatar Jul 25 '22 18:07 medvednikov

But the old code using error with ?Foo will keep working for 2 years.

medvednikov avatar Jul 25 '22 18:07 medvednikov

Sorry - had it backwards. Too much confusion with which punctuation to use when... :-\

JalonSolov avatar Jul 25 '22 19:07 JalonSolov