-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sum types should have a default 0 value for the first type mentioned in the list of types. #22030
Comments
The same is true for interface values too - they also do not have default values. For them, the problem is mitigated a bit because we do have an error for some situations involving them: a.v:2:6: error: cannot instantiate interface `Abc`
1 | interface Abc{}
2 | a := Abc{}
| ~~~~~
3 | dump(a)
#1 20:03:14 ^ master /v/oo> but others are still allowed: a.v:2:6: warning: arrays of interfaces need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`, or if you also use `init:`)
1 | interface Abc{}
2 | a := []Abc{len:4}
| ~~~~~~
3 | dump(a)
[a.v:3] a: [unknown interface value, unknown interface value, unknown interface value, unknown interface value] and even runtime errors, with no complains from the compiler: interface Abc{}
struct MyStruct { i Abc }
a := []MyStruct{len:4}
dump(a) producing:
|
One problem that will have to be solved too, when the first variant type of a sumtype declaration, gets used to define the default value, is that currently |
It could always alphabetize all the types after the first, but that seems like more trouble than it's worth. Changing the defaults for interfaces may also need it's own separate issue... seems like a much bigger problem than sum types. |
Just wanted to confirm that agree with this notion. |
Describe the bug
sum types do not have a default value. This can lead to errors if you try something as simple as
this could be easily fixed if the default was to assign the default 0 value for the first type in the list. Users could change which type was assigned by changing the order of types in the sum type.
Note that this change has been approved by Alex: https://discord.com/channels/592103645835821068/592106336838352923/1272226313314242673
Reproduction Steps
Expected Behavior
Current Behavior
Possible Solution
No response
Additional Information/Context
No response
V version
V 0.4.7 20a525a
Environment details (OS name and version, etc.)
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: