News for hackers. Stuff that matters.
Re:Notes on a Smaller Rust by skohan on Thursday July 18, 2019 @10:07AM (#20469897)
I haven't spent a lot of time with FP languages over the years, but implicit returns are one of my least favorite features of Rust. Especially the use of semicolons to denote a terminated statement vs. a return value: a semicolon-related typo is such an easy mistake to make, and an annoying reason for a build to fail, especially when so many modern languages have shown it's quite possible and pleasant to work without semicolons.
Re:Notes on a Smaller Rust by steveklabnik on Thursday July 18, 2019 @10:25AM (#20470081)
To be clear, Rust does not have "implicit returns" nor does it use semicolons to indicate return values.

Expressions evaluate to a value, and semicolons take an expression, throw away its value, and evaluate to () instead. That's it.

The other behaviors you're talking about fall out of these semantics, but are also different than what you've said; functions evaluate to a value, so the final expression's value determines the value it evaluates to, but you cannot "implicitly return" from the middle of a function by dropping a ;, for example.

Re:Notes on a Smaller Rust by _bxg1 on Thursday July 18, 2019 @10:28AM (#20470120)
What I meant was the fact that you can omit the "return" keyword:

  fn foo(x: i32) -> i32 { x * 2 }
Re:Notes on a Smaller Rust by steveklabnik on Thursday July 18, 2019 @10:47AM (#20470362)
Right, but only as the last expression in a block, which is different than what "implicit returns" implies.
Re:Notes on a Smaller Rust by skohan on Thursday July 18, 2019 @10:42AM (#20470309)
It's a bit of a semantic distinction isn't it? In practice what it means is that you add semicolons to the end of almost every line of imperative code in a block, and you leave the semicolon off the last line and this becomes your return value implicitly (without the use of a return keyword).
Re:Notes on a Smaller Rust by steveklabnik on Thursday July 18, 2019 @10:48AM (#20470383)
It is a semantic distinction, because "everything is an expression" and "implicit returns" have different semantics.

Thinking of them as implicit returns leads people to believe they can write code like

  fn foo(x: bool) -> i32 {
      if x {
          5
      }

      // more code
and then they're confused when this doesn't work.
Re:Notes on a Smaller Rust by _bxg1 on Thursday July 18, 2019 @11:49AM (#20471060)
I actually love the semicolon semantics. Making expressions and statements disjoint prevents bugs like this:

  if(foo = 5) {
    ...
  }
They are easy to mess up when you're getting started, but they're also very easy to fix because they'll basically always result in a clear compiler error. If "builds failing" are annoying for you, you probably aren't using an IDE with inline errors, and with Rust you absolutely should be doing that or you'll have a bad time.
Re:Notes on a Smaller Rust by skohan on Thursday July 18, 2019 @12:13PM (#20471296)

   if(foo = 5) {
       ...
   }
You don't need the rust style expression/statement distinction to catch this. Many languages will catch this at compile time since "foo = 5" does not evaluate to a boolean.

I know that it's relatively easy to catch this error in Rust, and it is at most a minor annoyance, but it still feels like a clear step backwards after working with languages which don't require semicolons at all.

Re:Notes on a Smaller Rust by _bxg1 on Thursday July 18, 2019 @12:20PM (#20471365)
> since "foo = 5" does not evaluate to a boolean

In most C-like languages, this expression is valid: the whole thing evaluates to "5", which has a truthiness. Some "clever" programmers use this pattern intentionally in loops to save a line of code, especially in the C/C++ world. For example:

  while(currentNode = currentNode.next) {
    ...
  }
When currentNode becomes null, the expression/statement becomes falsy, and the loop terminates. I absolutely love the way Rust systematically disallows shaky syntax tricks like this.
Re:Notes on a Smaller Rust by skohan on Thursday July 18, 2019 @12:25PM (#20471423)
Yeah I know, but there's a wide range of languages which are neither C nor Rust.

My point is, there are plenty of examples of languages that show that you don't need to add semicolons to almost every line of your program just to avoid this one potential error.

Re:Notes on a Smaller Rust by leshow on Thursday July 18, 2019 @04:06PM (#20473454)
Out of curiousity, which languages are you referring to? Assignment in if-statements is possible in C, C++, js, Go, and many other C-like languages.

I think the only language I know of that disallows assignment in them is python, and it's not because assignment evaluates to something other than boolean, python is happen to evaluate the 'truthiness' of certain types. And funnily enough, they are going to add this back in to python 3 I believe, using a different operator.

Re:Notes on a Smaller Rust by skohan on Thursday July 18, 2019 @05:05PM (#20473913)
Swift, Kotlin, D, C# to name a few.

Are you sure about Go? I just tried this in an online playground, and I get this error:

    syntax error: assignment x = 2 used as value