V version:
latest master
OS:
win10
What did you do?
type Foo = int | f64
struct Bar {
foo Foo
}
fn main() {
a := Bar{foo: int(1)}
match a.foo {
int {
println(a.foo)
}
f64 { }
}
}
What did you expect to see?
error, because there's shadowing but not matching a simple variable (Instead, matching a struct member).
What did you see instead?
cgen error: could not generate string method main_Foo_str for type 'main_Foo'
I think this is essentially working as expected whilst we support it, although Alex wants it to be removed. After removal this can be an error - 'use as name'.
Edit: although sometimes you don't need to access the value, you only need the type matching.
a.foo is the same as outside the match statement. The println error should be a V error though.
@ntrel Yeah with as name it works as expected, but isn't this more like a workaround?
match a doesn't require as name whereas match a.foo requires it. - Is this on purpose in V?
match a doesn't require as name whereas match a.foo requires it.
match a.foo { is actually valid, you just can't access the sum type element easily (without it) . Sometimes you don't need to though, you just want to react to what the element type is.
Is this on purpose in V?
Yes, from the docs:
Note: shadowing only works when the match expression is a variable. It will not work on struct fields, arrays indexing, or map key lookup.
@ntrel Thanks. Issue topic changed.
match struct member without using
as nameshould give error
I don't think it should be an error as I explained, even after it is removed.
There is a bug about a missing str method for sum types though. I think V could generate that automatically.
as is supposed to be used mostly for a.foo (fields), but I think perhaps we should just allow
match a.foo { int { println(a.foo + 1) } }
Then as can be removed.
Then as can be removed
Not if the condition is a function call match get_foo() {.
I don't think shadowing a.foo is a good idea. Shadowing a variable is fine, conceptually we just declare a new variable with the same name. Shadowing a field of a struct instance is not even possible in C. I expect there would be problematic corner cases.