Remix.run Logo
3eb7988a1663 2 hours ago

Never will I understand ternary operators. As soon as you introduce it, some chuckle heads want to use them everywhere. Worse if the syntax allows nested ternarys. I guess it keeps the language open for code golfing, but it otherwise seems like redundant syntax that at best saves a few characters.

201984 2 hours ago | parent | next [-]

Lua basically already has ternary operators anyway since "and" and "or" short circuit. I also don't see the need of adding additional syntax for it.

  local x = condition ? value_a : value b
  local x = condition and value_a or value_b
matheusmoreira 2 hours ago | parent [-]

> The classic Lua idiom a and b or c has a pitfall when b is nil or false: then c is returned, even when a is truthy.

> E.g. true and false or 42 returns 42, whereas true ? false : 42 returns the (expected) false.

demilicious 2 hours ago | parent | prev | next [-]

That’s why “if” should just be an expression

NuclearPM 9 minutes ago | parent | next [-]

Yep. Everything should be.

matheusmoreira 2 hours ago | parent | prev [-]

This is the best answer in my opinion. Ternary is just sugar for an expressive if. LuaJIT seems to be focusing on adding new syntax though, maintainer might not be amenable to updating existing semantics.

wavemode an hour ago | parent [-]

I don't think if-expressions have to affect existing semantics. Basically, in the parser you would have two different kinds of AST nodes, one for when the `if` keyword is encountered in statement position and another for when it's encountered in expression position.

Right now, `if` in expression position is just a syntax error ("unexpected symbol")

hiccuphippo 2 hours ago | parent | prev | next [-]

I find it most useful in languages that have non-mutable variables and you want to avoid a mutable variable or an extra function when the value comes from a simple condition.

Gualdrapo 2 hours ago | parent | prev [-]

I guess for the JS case it makes sense to be able to shave a few characters for file shrinking purposes, but generally I'm more biased to code clarity and "self-explainability"

NuclearPM 7 minutes ago | parent [-]

That’s what compression is for.