V version: V 0.1.29 a2fc198, commited at 2020-11-04 16:02:01 +0200
OS: Windows 10 WSL2 Ubuntu 20.04
What did you do?
type Bar = string | int
fn main() {
x := Bar('test')
_ := x == 'test' // < & > result in error as well
}
What did you expect to see?
and V compiler error
What did you see instead?
/tmp/v/main.6873198028408993285.tmp.c:8023:14: error: invalid operands to binary expression ('main__Bar' and 'string' (aka 'struct string'))
{bool _ = x == tos_lit("test");}
~ ^ ~~~~~~~~~~~~~~~
I think x == 'test' could work and produce false if x is not a string.
it can't work.
Why not?
because x is a sumtype, can't be checked against a string.
You know the right hand side is a string, so you can check (at runtime) the element type corresponds to string and generate code to compare the sum type element as a string.
not that important right now.
we can do
if x is string {
assert x == 'test'
}
βxΒ β=β=βΒ β'βtestβ'β != βx is string &&Β βxβΒ β=β=βΒ β'βtestβ'β, that check shouldn't be done implicitly imho.
βx is string && βxβ β=β=β β'βtestβ'β, that check shouldn't be done implicitly imho.
I see your point, and I agree that type inference is a very nice thing to have, but code clarity is important too :)
While I don't personally dislike your suggestion, I'm not sure it fits in well with the "V is not about saving keystrokes, code readability has a much higher priority" idea.
It's ultimately up to @medvednikov to decide if that should be implemented or not.
A few notes about your
V is not about saving keystrokes
None of my reasons were about saving keystrokes.
shadowing most complex expressions is already possible with #6745
Not with a function call. Also field shadowing can't work when the receiver is mutable - see: https://github.com/vlang/v/pull/6792#discussion_r521326245
this gives now:
main.v:5:9: error: cannot use operator `==` with `Bar`
3 | fn main() {
4 | x := Bar('test')
5 | _ := x == 'test' // < & > result in error as well
| ~~
6 | }
Most helpful comment
βxΒ β=β=βΒ β'βtestβ'β!=βx is string &&Β βxβΒ β=β=βΒ β'βtestβ'β, that check shouldn't be done implicitly imho.