Skip to content
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

Closed
JalonSolov opened this issue Aug 11, 2024 · 4 comments · Fixed by #22039
Closed
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@JalonSolov
Copy link
Contributor

JalonSolov commented Aug 11, 2024

Describe the bug

sum types do not have a default value. This can lead to errors if you try something as simple as

x := MyType{}

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

type MyType = int | string

x := MyType{}

println(x)

Expected Behavior

0

Current Behavior

x.v:3:6: notice: direct sum type init (`x := SumType{}`) will be removed soon
    1 | type MyType = int | string
    2 | 
    3 | x := MyType{}
      |      ~~~~~~~~
    4 | 
    5 | println(x)
unknown sum type value

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.7 20a525a

Environment details (OS name and version, etc.)

V full version: V 0.4.7 efa98d9.20a525a
OS: linux, "Manjaro Linux"
Processor: 32 cpus, 64bit, little endian, AMD Ryzen 9 7950X 16-Core Processor

getwd: /home/jalon
vexe: /home/jalon/git/v/v
vexe mtime: 2024-08-11 16:45:16

vroot: OK, value: /home/jalon/git/v
VMODULES: OK, value: /home/jalon/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.32-24-g20a525ae
.git/config present: true

CC version: cc (GCC) 14.1.1 20240720
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

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.

@JalonSolov JalonSolov added the Bug This tag is applied to issues which reports bugs. label Aug 11, 2024
@spytheman
Copy link
Member

spytheman commented Aug 11, 2024

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:

#148 20:05:19 ^ master /v/oo>v run a.v
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:2277: at indent_main__Abc_str: RUNTIME ERROR: invalid memory access
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:2264: by indent_main__MyStruct_str
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:2216: by indent_Array_main__MyStruct_str
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:2210: by Array_main__MyStruct_str
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:2233: by _v_dump_expr_Array_main__MyStruct
/tmp/v_1000/a.01J516DMX1F1X3GBBFX1R951GE.tmp.c:13400: by main__main

@spytheman
Copy link
Member

spytheman commented Aug 11, 2024

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 v fmt -w . will reorder the sumtype variants in alphabetical order, and it should not, since the first type will have a semantic meaning in the future.

@JalonSolov
Copy link
Contributor Author

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.

@medvednikov
Copy link
Member

Just wanted to confirm that agree with this notion.

@felipensp felipensp self-assigned this Aug 12, 2024
@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants