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

Cannot use $if/$else to set value of enum. #23336

Open
islonely opened this issue Jan 1, 2025 · 5 comments
Open

Cannot use $if/$else to set value of enum. #23336

islonely opened this issue Jan 1, 2025 · 5 comments
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@islonely
Copy link
Contributor

islonely commented Jan 1, 2025

V doctor:

V full version: V 0.4.9 4225a34.42222e6
OS: macos, macOS, 15.1.1, 24B91
Processor: 8 cpus, 64bit, little endian, Apple M3

getwd: /Users/adamoates/.vmodules/islonely/mouse
vexe: /Users/adamoates/v/v
vexe mtime: 2025-01-01 19:24:10

vroot: OK, value: /Users/adamoates/v
VMODULES: OK, value: /Users/adamoates/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: weekly.2024.53-6-g42222e6c
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

What did you do?
./v -g -o vdbg cmd/v && ./vdbg /Users/adamoates/Documents/test.v

enum Foo {
	foo = $if macos { 1 } $else { 0 }
}
fn main() {}

What did you expect to see?

Expecte enum Foo.foo value to be 1 on MacOS and 0 on other systems.

What did you see instead?

/Users/adamoates/Documents/test.v:2:8: error: the default value for an enum has to be an integer
    1 | enum Foo {
    2 |     foo = $if macos { 1 } $else { 0 }
      |           ~~~
    3 | }
    4 | fn main() {}

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.

Huly®: V_0.6-21767

@felipensp felipensp added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Jan 1, 2025
@Delta456
Copy link
Member

Delta456 commented Jan 2, 2025

enum Foo {
	foo = if macos { 1 } else { 0 }
}
fn main() {}

The above code gives an error so ternary conditions is not supported entirely for enum fields

@jorgeluismireles
Copy link

Imho I'll be happy using a method:

enum Foo {
	foo
}

fn (f Foo) val() int {
	match f {
		.foo {
			return $if macos { 1 } $else { 0 }
		}
	}
}

fn main() {
	println('${Foo.foo.val()}') // prints 0
}

@jorgeluismireles
Copy link

Another workaround

const x = $if macos { 1 } $else { 2 }

enum Foo {
	foo = x
}

fn main() {
	println('${Foo.foo:d}') // prints 2
}

@jorgeluismireles
Copy link

jorgeluismireles commented Jan 2, 2025

But then, a bug kicks in, next program causes two enum members have the same value:

const x = $if macos { 1 } $else { 2 }
const y = $if macos { 3 } $else { 2 }

enum Foo {
	foo = x
	bar = y
}

fn main() {
	println('${Foo.foo:d}') // prints 2
	println('${Foo.bar:d}') // prints 2
}

A problem which is catch when we type directly a repeated value:

code.v:6:7: error: enum value `2` already exists
    4 | enum Foo {
    5 |     foo = 2
    6 |     bar = 2
      |          ^
    7 | }
    8 |
}

@medvednikov
Copy link
Member

Great find!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants