[Design] Multiline expression
Hey,
At the end of https://github.com/sharkdp/numbat/pull/519 you said:
The lookahead would also be useful for common expressions like
let my_result = some * complicated * terms + more * complicated * stuffwhere the `+' is on the next line. But not sure if that's really possible to do with the current mechanism.
And I think that would be really nice so I’m just opening an issue to keep track of it. For example converting:
fn _human_readable_duration(time: Time, dt: DateTime, num_days: Scalar) -> String =
_human_days(_human_num_days(time)) |> _human_join(_human_hours(dt)) |> _humain_join(_human_minutes(dt)) |> _human_join(_human_seconds(dt))
to
fn _human_readable_duration(time: Time, dt: DateTime, num_days: Scalar) -> String =
_human_days(_human_num_days(time))
|> _human_join(_human_hours(dt))
|> _human_join(_human_minutes(dt))
|> _human_join(_human_seconds(dt))
Thank you for writing it down. It's probably good to do some research into what other programming languages (without semicolon-terminated statements) do, before we implement this. For example: Python requires \-escaped newlines in some cases (when?).
It probably helps that we decided to use introducer-keywords like let or fn in Numbat. Otherwise, it would be hard to decide what something like this is:
some * terms +
my_variable = 2
Huum just a though but maybe that’s not that big of a deal if we provide a formatter (especially since numbat already sends you back what it understood of your statement). But yeah, I would say that while we continue to use introducer-keywords, we should be good.
Ooops, just saw this old issue. I proposed a solution here: https://github.com/sharkdp/numbat/issues/549#issuecomment-3369338140
The TLDR is: We consider an expression starting with a newline to be a multi-line expression.
And multi-line expression should end with a new end keyword.
Ooops, just saw this old issue. I proposed a solution here: https://github.com/sharkdp/numbat/issues/549#issuecomment-3369338140
The TLDR is: We consider an expression starting with a newline to be a multi-line expression. And multi-line expression should end with a new
endkeyword.
As I said in response to the linked comment I like the idea. As it pertains to multi-line expressions I have a question. How would we define
an expression starting with a newline
You gave the following examples:
(* This one is good *) if X then bidule else truc (* This is also good *) if X then bidule else truc end (* This is clearly bad *) if X then bidule (* error here, you started an expression and have to finish on the same line *) else truc end (* Should this be good? The newline was inserted at the start of a newline *) if X then bidule else truc end (* I would say no, the newline should have been inserted right after the `then` *)
I'm not sure I would like as strict rules as you propose in the last two cases. My reasoning is it would make it easier to define a heuristic for what constitutes a multi-line expression, communicate the restrictions and to allow more flexibility for the end users.
My proposal for a heuristic would be something like "a newline after an if-then-else keyword or a = makes the expression that follows a multi-line expression"
This would make it easier to handle else-if chains and allow the flexibility to do something like the following:
if
a == b
&& a == c
&& b != c
then "logic fail"
else "logic success"
My proposal for a heuristic would be something like "a newline after an if-then-else keyword or a = makes the expression that follows a multi-line expression"
Yes makes sense 👍
I terms of code I think it's something like "in every statement, every time we're trying to parse an expression it can start with a newline and make the whole statement multiline"