-
-
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
Cannot use $if/$else to set value of enum. #23336
Labels
Feature/Enhancement Request
This issue is made to request a feature or an enhancement to an existing one.
Comments
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
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 |
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
} |
Another workaround const x = $if macos { 1 } $else { 2 }
enum Foo {
foo = x
}
fn main() {
println('${Foo.foo:d}') // prints 2
} |
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:
|
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.
V doctor:
What did you do?
./v -g -o vdbg cmd/v && ./vdbg /Users/adamoates/Documents/test.v
What did you expect to see?
Expecte enum
Foo.foo
value to be1
on MacOS and0
on other systems.What did you see instead?
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
The text was updated successfully, but these errors were encountered: