Hacker News new | past | comments | ask | show | jobs | submit login

I'll admit I'm not a Ruby developer, but wow it allows return conditionally inside an expression? What were the language designers thinking?! I can't think of any other language that allows return inside an expression (or break/continue). Let's see: C, C++, C#, Python, Javascript, Object Pascal, Java, Rust; all nope. That is indefensible.



You've listed languages that separate statements from expressions in their syntax. There is a whole other family of languages where choose consists entirely of expressions - starting with LISP in the 50s, and including virtually all FP languages today (F#, Ocaml, Haskell, SML, Clojure). Ruby happens to be one a member of the latter group. To be fair, most of the languages in that group don't have an equivalent of return/break/continue either - I think only Ruby and some Lisps do.

Regardless, all of the languages above except C and maybe Object Pascal can still have control stop in the middle of an expression, since an expression can throw an Exception (indirectly). Returning is not significantly different from that.


> I can't think of any other language that allows return inside an expression ... Rust

Er, what?

You understand almost everything in Rust is an expression right?

    let x = if yeah_nah() { return 5; } else { "X" };
That return is an expression, obviously we mostly care about the expression's side effect which is a change of control flow to leave the function with some sort of integer 5 as the return value (the function signature tells Rust which kind of integer this 5 is) - but the expression "return 5" itself does have a type, it's ! aka Never, an Empty Type because no values of this type can exist - because the side effect will change the control flow before we need a value.

Rust's type inference engine is fine with this because under type arithmetic all the values fit in a single type, the type of "X" - &'static str - there are no values on the left to disrupt that.


It's not doing "return conditionally inside an expression". The above statement is equivalent to:

   if !condition
     return
   end
In other words, the return in question has no argument. The unusual (but not unique) aspect of Ruby here is that if/unless can suffix the "then" block and not just prefix it.


If and unless can be used as post-fix clauses. Also, in ruby, -everything- is an expression, so... of course you can return from there


This isn't exactly true. It's a syntax error to put a return anywhere where the grammar expects a value expression, because it's one of few constructs in Ruby that doesn't return a value (contrast with e.g. "if" and "def" and "class", all of which return a value).

E.g. "42 == (return true)" is a syntax error while "42 == if false; true; end" is syntactically valid.

You can return in the expression in the comment above because the return is at statement level - nothing above it in the grammar requires a value (but obviously allows it).

Off the top of my head I can't remember which other keywords fall in that category. Obviously "end", "then", "alias" and there'll be a few more, but for most keywords in Ruby you're right they can be treated as expressions.


Nah, it's pretty cool once you get used to it


Kotlin has this, check out https://kotlinlang.org/docs/returns.html




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: