Post B2zN4VzFq2XFcMc5wm by pervognsen@mastodon.social
(DIR) More posts by pervognsen@mastodon.social
(DIR) Post #B2zN4SIFZTLyA7TD04 by pervognsen@mastodon.social
2026-02-04T14:32:41Z
0 likes, 0 repeats
The more I use them, the more I'm convinced that, for most code with non-trivial case analysis, let-variables with exactly-once definite assignment analysis are usually ergonomically superior to the expression-oriented value yielding typical of functional languages. It's especially true as soon as you find yourself tupling things just for the sake of yielding multiple values in an expression-oriented way, which I always found awful.
(DIR) Post #B2zN4TjEEeWmc56Ggq by pervognsen@mastodon.social
2026-02-04T15:03:13Z
0 likes, 0 repeats
Also, with every code editor nowadays having one-button Find References, you can find all the let assignments on different control flow paths when hovering the cursor over a given local variable name. Names are nice for connecting producers and consumers.If your language has complete type inference there's also no difference with respect to let variable assignments vs expression-oriented code. Rust has incomplete type inference so some code patterns won't preserve inferability, but most will.
(DIR) Post #B2zN4UxRfHUgQSl8UK by pervognsen@mastodon.social
2026-02-04T15:18:34Z
0 likes, 0 repeats
You can write eitherlet (min, max) = if x <= y { (x, y) } else { (y, x) };orlet min;let max;if x <= y { min = x; max = y;} else { min = y; max = x;}orlet min;let max;if x <= y { (min, max) = (x, y);} else { (min, max) = (y, x);}and it will infer equally. That example isn't compelling for the assignment-based alternatives (I'd use the expression-oriented one-liner) but it makes a big difference when the case analysis and nesting gets more complex.
(DIR) Post #B2zN4VzFq2XFcMc5wm by pervognsen@mastodon.social
2026-02-04T15:22:40Z
0 likes, 0 repeats
For that matter, I just noticed you can replacelet min;let max;withlet (min, max);which I hadn't tried before (of course it's common to do that when you have an initializer on the right-hand side of the let). Since it also works with let [min, max]; I guess any well-formed irrefutable pattern works without a let initializer, which is a bit funny-looking but I guess makes sense.
(DIR) Post #B2zN4WtGTnL2Q4opFY by wolf480pl@mstdn.io
2026-02-04T20:33:06Z
0 likes, 0 repeats
@pervognsendoes it work with refutable ones, like let Some(min);?
(DIR) Post #B2zNEdFDi4rJ1RuCNk by pervognsen@mastodon.social
2026-02-04T20:34:57Z
0 likes, 0 repeats
@wolf480pl Nope but neither would do let Some(min) = rhs; unless you wrote it as let Some(min) = ... else { ... }; and that syntactic production does require the = ... part.