News for hackers. Stuff that matters.
Re:Notes on a Smaller Rust by lmm on Thursday July 18, 2019 @07:46AM (#20468753)
Interesting. Coming from an FP background I'd say the opposite - the confusing part of Rust is all these special-case imperative control flow keywords, a more smalltalk-like "everything is just values and functions" syntax would be the best way to simplify it.

I'd agree with stepping away from guarantees about when allocations happen etc. But I think at the point where you remove those from Rust you really just have OCaml.

Re:Notes on a Smaller Rust by _bxg1 on Thursday July 18, 2019 @09:51AM (#20469741)
I think one thing that confuses people about Rust is it sends mixed signals with regard to functional vs imperative. It gives you lots of nice functional toys - closures, implicit returns, pattern matching - but you end up having to write a lot of imperative stuff too.

I've found that Rust flows much more nicely when you just embrace an imperative style "by default". That's its native tongue; the functional stuff is more of an exception.

Those of us used to multi-paradigm languages have taught ourselves that imperative == bad (actual for-loops! egads!), but the thing is, Rust is specifically designed to make it not-bad. It gives you all these tools to carefully control and limit mutability, so you can do imperative stuff with much less fear. It takes getting used to but in my (limited) experience this mindset significantly reduces friction when working with Rust.

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
Re:Notes on a Smaller Rust by cmrdporcupine on Thursday July 18, 2019 @07:57AM (#20468836)
Well, it'd be OCaml without a garbage collector. And to be honest, that would be a very useful and handy language for systems development.
Re:Notes on a Smaller Rust by lmm on Thursday July 18, 2019 @09:23AM (#20469475)
OCaml is already a good language for systems development - people are far too scared of garbage collectors. And if you make the changes in the article then I think you give up most of the concrete benefits of non-GC - you won't have easy C interop without control of stack/heap/allocation, and since trait objects could end up nested arbitrarily far, I think you might still get GC-like pauses where a simple-looking code line ended up doing an arbitrary amount of unwrapping and freeing work.
Re:Notes on a Smaller Rust by cmrdporcupine on Thursday July 18, 2019 @10:32AM (#20470181)
I guess it depends on your definition of systems development. If you mean "writing systemsy applications" like IoT things or whatever, sure, fine, a GC isn't going to be an issue and probably will help.

If you're talking about a language to write drivers or an operating system, etc in then a GC is just a big No. In that domain we don't even have Malloc/Free, let alone GC services. Having the overhead and normative lifestyle assumptions of _any_ kind of runtime is a big Nope.

Re:Notes on a Smaller Rust by atombender on Friday July 19, 2019 @08:37AM (#20478086)
Counterpoint: The Oberon System successfully implemented a garbage-collected OS kernel: https://en.wikipedia.org/wiki/Oberon_(operating_system).
Re:Notes on a Smaller Rust by wiml on Thursday July 18, 2019 @09:22PM (#20475497)
Mirage exists and works reasonably well.
Re:Notes on a Smaller Rust by masklinn on Thursday July 18, 2019 @08:08AM (#20468908)
OCaml has for and while loops. Its for loop is even more imperative than Rust's (which simply drives an iterator, whereas OCaml's has "for a to b" and "for b downto a").

OCaml doesn't have break or continue though.

Re:Notes on a Smaller Rust by 0815test on Thursday July 18, 2019 @08:26AM (#20469013)
> a more smalltalk-like "everything is just values and functions" syntax

Can you do that sort of thing without a GC? Some functional languages use tailcall as their control flow primitive, but that's just a glorified GOTO.

Re:Notes on a Smaller Rust by lmm on Thursday July 18, 2019 @09:26AM (#20469498)
I don't see why not? Rust has first-class functions and its lifetime support is supposed to be a big selling point, so it ought to be possible to implement things like "if" and "for" as userspace functions - and if not, then that suggests important limitations to its lifetime/ownership model.
Re:Notes on a Smaller Rust by viraptor on Thursday July 18, 2019 @08:01AM (#20468856)
> special-case imperative control flow keywords,

Which ones do you mean?

Re:Notes on a Smaller Rust by masklinn on Thursday July 18, 2019 @08:07AM (#20468902)
Not OP but from what they're saying I'd guess loop, while, for, continue, break, return. Possibly do (which is reserved but not used), could be a do{}while or a monadic block, who knows.

Though it should be noted that Smalltalk provided a non-local return.