-
-
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
Sharp edges of the "ternary" operator #1907
Comments
Hi @dumblob, we don't actually have a ternary operator. Also the syntax is wrong it should be:
|
Yep, that's why I put it in double quotes (though it compiles down currently to a plain ternary operator in C).
Yep, I expected something like that - that's why I made a set of examples which currently feel right according to the scarce information about V syntax & semantics. Once the syntax & semantics settles, we'll need to soften these sharp edges and make them working in some way to avoid "wtf" moments when exploring the language with the strategy: "V allows terse readable code, let's write that way" (i.e. shortest possible still readable code - i.e. panicking immediately instead of additionally returning a dummy
I'll correct it. It's though interesting, than V didn't complain about it and in one case at least compiled the code without |
|
Also if expression is very unstable as for now. You can use match expression. |
@Danil-Lapirow Are you deliberately using import rand
fn f() ?int {
rand.seed( 0 )
if rand.next( 9 ) > 5 { return 3 }
return error( 'rand was not greater than 5' )
}
fn main() {
res := f() or { panic(err) }
println( if res <= 5 { 6 } else { 7 } )
} But yes, I agree that there are many small nuances in V now that are not handled in compiler and then results in errors in C compiler. I think the reason for that is simplified parsing and code generation without the AST and a rigorous analysis of it. I understand that looked nice and easy for @medvednikov in the start and made things run quicker also, but it scares me when thinking about the reliability of V in the future. This kind of ad-hoc aproach to fighthing errors in compiler without proper rigorous tests to avoid regressions is the biggest risk from my point of view regarding the bright future of V. |
Could an explicit "else" help? if rand.next( 9 ) > 5 {
return 3
}
else {
return error( 'rand was not greater than 5' )
} In manner to en--force--courage to write code the same way, I think an "if" without following "else" shall be not valid (as a "match" without an default case). |
@gslicer No. There are many situations when |
This "nothing" is exactly expressed with an empty else {} clause - but OK, maybe it makes sense to have some optional syntax and not enforce consistency all over the place |
Looks like the errors are caused by:
|
@lutherwenxu what info is still needed? I also fail to find any questions here. This is a pure buggy behavior IMHO 😉. |
Fixed. |
@medvednikov I might be missing something, but many cases still do not seem to work: case 1import rand
fn f() ?int {
rand.seed( 999 )
return if rand.next( 9 ) <= 5 { error( 'ERR <=5' ) } else { 3 }
}
fn main() {
x := if f()? <= 5 { 6 } else { 7 }
println( x )
} yields:
case 2import rand
fn f() int {
rand.seed( 999 )
x := if rand.next( 9 ) > 5 { 3 } else { 4 }
return x
}
fn main() {
x := if f() or { 5 } <= 5 { 6 } else { 7 }
println( x )
} yields:
case 3 (basically the same as case 1 but with temporary variable)import rand
fn f() ?int {
rand.seed( 999 )
x := if rand.next( 9 ) <= 5 { error( 'ERR <=5' ) } else { 3 }
return x
}
fn main() {
x := if f()? <= 5 { 6 } else { 7 }
println( x )
} yields:
case 4 (function lies it returns an Optional but in fact it doesn't)import rand
fn f() ?int {
rand.seed( 999 )
return if rand.next( 9 ) > 5 { 3 } else { 4 }
}
fn main() {
x := if f()? <= 5 { 6 } else { 7 }
println( x )
} yields (but should produce a different error - namely that the function in fact doesn't return any Optional despite having the return type specified as such):
case 5 (same as case 4, but with a temporary variable)import rand
fn f() ?int {
rand.seed( 999 )
x := if rand.next( 9 ) > 5 { 3 } else { 4 }
return x
}
fn main() {
x := if f()? <= 5 { 6 } else { 7 }
println( x )
} yields:
|
V version:
V 0.1.18 (git commit
* 7fc678c 2019-09-09 fix urllib_test.v (HEAD -> master, origin/master, origin/HEAD) [Alexander Medvednikov]|
)OS:
Linux t500 4.19.67-1-lts #1 SMP Fri Aug 16 10:30:45 CEST 2019 x86_64 GNU/Linux
What did you do?
Neither of the following works. Each produces a very different error and this error is either fully misleading or a syntax generation bug in V compiler. Some of them may be related to #1108 though V compiler didn't produce any such indicative error.
What did you expect to see?
All of them are IMHO a valid syntax in V. So all of them shall be correctly compiled.
What did you see instead?
Many different wild errors.
P.S. It would be nice to make a test out of each reported issue and making it a commit policy to run all these regression tests first before merging to master.
The text was updated successfully, but these errors were encountered: