V: sum type infix operators result in c error

Created on 4 Nov 2020  Β·  12Comments  Β·  Source: vlang/v

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");}
                  ~ ^  ~~~~~~~~~~~~~~~
Bug

Most helpful comment

​x ​=​=​ ​'​test​'​ != ​x is string && ​x​ ​=​=​ ​'​test​'​, that check shouldn't be done implicitly imho.

All 12 comments

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.

  1. x may be a complex expression, repeating it can have side effects so you actually would need to declare a variable.
  2. Putting the type instead of inferring it is not even allowed for variable declarations. V should encourage type inference.
  3. It might be hard to implement variable shadowing for x when the expression occurs inside a complex expression.
  1. Optionals benefit too: `opt == 5'. There is no expression to do this safely for optionals.

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

  1. Smartcasting / shadowing most complex expressions is already possible with #6745, and

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 | }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  Β·  3Comments

taojy123 picture taojy123  Β·  3Comments

XVilka picture XVilka  Β·  3Comments

arg2das picture arg2das  Β·  3Comments

cjmxp picture cjmxp  Β·  3Comments