v icon indicating copy to clipboard operation
v copied to clipboard

Cgen `error: cannot convert 'struct _option_int' to 'int'`

Open i582 opened this issue 2 years ago • 5 comments

Describe the bug

See code: https://play.vlang.io/p/ab17c4b2cc

interface IFoo {
	age ?int
	get(param ?int) ?int
}

struct Foo {
	age ?int
}

fn (f Foo) get(param ?int) ?int {
	return param or { f.age }
}

foo := IFoo(Foo{age: 100})
println(foo.get(100))

Expected Behavior

No error

Current Behavior

Cgen error

Reproduction Steps

Run code above

Possible Solution

No response

Additional Information/Context

No response

V version

0.3.3

Environment details (OS name and version, etc.)

Playground

i582 avatar Mar 02 '23 05:03 i582

fn (f Foo) get(param ?int) ?int {
	return param or { f.age? }
}

Adding ? to the end of f.age seems to work.

lcddh avatar Mar 02 '23 15:03 lcddh

fn (f Foo) get(param ?int) ?int {
	return param or { f.age? }
}

Adding ? to the end of f.age seems to work.

Yes, but it must throws error without unwrapping the option.

felipensp avatar Mar 02 '23 15:03 felipensp

f.age is supposed to be of type ?int, so it should be able to return directly

lcddh avatar Mar 02 '23 15:03 lcddh

f.age is supposed to be of type ?int, so it should be able to return directly

Not exactly. or block must resolve to unwrapped expected type, not to another option one. This is as V planned it.

felipensp avatar Mar 02 '23 15:03 felipensp

f.age is supposed to be of type ?int, so it should be able to return directly

Not exactly. or block must resolve to unwrapped expected type, not to another option one. This is as V planned it.

ok,I got it.

lcddh avatar Mar 02 '23 15:03 lcddh