Comments (6)
A smaller example with a similar problem:
>>> if true then 1 |> id else 0
error: while parsing
┌─ <input:11>:1:16
│
1 │ if true then 1 |> id else 0
│ ^^ Expected 'else' in if-then-else condition
The problem is that |>
has lower precedence than if … then … else
(https://numbat.dev/doc/operations.html). It can be fixed using parens:
>>> if true then (1 |> id) else 0
But maybe precedence should be changed to allow for this? I guess if we would do that, it would prevent us from doing things like
if … then … else … |> function
and we would have to put the conditional into parens:
(if … then … else …) |> function
which defeats the initial purpose of this operator (that you can always just add it at the end of a line to do an additional function call)
from numbat.
Unrelated: I'm excited about this unit_list
feature!
from numbat.
The problem is that |> has lower precedence than if … then … else
Oh of course, I completely forgot about that. I'm not sure of which is better, but perhaps a terminating token to the if-else expression would solve some of the problem? My thinking is that the then clause is already delimited by the else, making it a logical unit to scope as if in parentheses. If the else clause could be delimited as well it would also have an easy to parse scope. It does bring other challenges but it would disambiguate a pipe after the whole statement as if it's after the delimiter it would apply to the whole if-else expression while if inside it applies to only that scope. My main argument against this approach would probably be changing if-else expressions would become ugly or reintroduce ambiguity.
For good measure here is an example syntax for the idea:
if true
then 1.5m |> floor_in(m)
else 1.5m |> ceil_in(m)
end
# equivalent to
if (true)
then ( 1.5m |> floor_in(m) )
else ( 1.5m |> ceil_in(m) )
end
# the problem with chaining:
if i < 0
then "negative"
else
if i > 0
then "positive"
else "zero"
end
end
from numbat.
Unrelated: I'm excited about this unit_list feature!
I really liked the idea of it when I read about it, so I'm excited to try my hand at it!
from numbat.
I'm not sure of which is better, but perhaps a terminating token to the if-else expression would solve some of the problem? My thinking is that the then clause is already delimited by the else, making it a logical unit to scope as if in parentheses. If the else clause could be delimited as well it would also have an easy to parse scope. It does bring other challenges but it would disambiguate a pipe after the whole statement as if it's after the delimiter it would apply to the whole if-else expression while if inside it applies to only that scope.
I think that reasoning is correct.
if-else expressions would become ugly
Indeed :-/
# the problem with chaining:
what do you mean by that?
from numbat.
# the problem with chaining:
what do you mean by that?
I meant that making a chain of if-else expression would no longer be possible without nesting them.
I've had an idea about this. Could the syntax of the if-else be extended? To my understanding the current syntax is something like the following.
IfElse = "if" Cond "then" Expr "else" Expr
What if chaining was an actual part of the syntax and all chains had to end with a terminating token. Something like the following.
IfElse = "if" Cond "then" Expr "else" Expr ; Regular if-then-else
IfElseChain = 1*IfElse "end" ; A chain of one or more if-then-else's terminated with a single "end" token
I'm thinking this allows for an optional end token for the normal case that could be used to make the parsing of the pipe operator unambiguous and making the nesting of if-then-else chains easier to determine. As far as I can see it would allow for the current syntax in all cases but enable additional uses without the need for parentheses or many ugly terminating tokens.
Disclaimer: I do not really understand the current implementation of the expressions. I have never used Rust and can't really gauge how big of an undertaking this would be, so if it is a larger change then I don't mind the parentheses that much, but would probably write a paragraph about it in the documentation.
from numbat.
Related Issues (20)
- Unexpected behavior of unit conversions inside functions HOT 4
- Why are we calling `FullSimplify` everywhere HOT 1
- Notepad/soulver-like interface for Numbat? HOT 10
- [Design] Multiline expression HOT 2
- Return the position of runtime failure HOT 3
- Local bindings are missing in relevant matches for typed holes
- Pretty printing shows superflous forall quantifiers
- Formatting integer output without underscores HOT 2
- Tests fail without network access HOT 2
- Error on unit definition using square roots HOT 2
- Panic when using `abs()` with a non-scalar argument HOT 2
- Unclear assertion errors due to floating point numbers HOT 1
- Improve identifier clash errors when conflicting identifier is declared in `@aliases` decorator
- Allow .nbt files to be loaded in REPL environment HOT 3
- Shift-enter does not insert newline (opt/alt-enter does)
- Fallback to implicit multiplication when function call fails? HOT 2
- Document MSRV HOT 1
- Usage as a Rust library for runtime string parsing HOT 1
- Constant: tau HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from numbat.