One of the goals behind Elixir is to provide a certain amount of syntax
that translates to a clear abstract syntax tree. The syntax available to
create the language should also be available for others to extend language.

Operators are generally tricky because they are hardcoded in the language.
You can’t smash tokens together to create a new operator (nor would that be
desired), in contrast to how we can smash letters together to create new
functions. So operators by definition are less extensible, which we want to
avoid, and Elixir partially addresses that my providing a set of operators
without meeting.

Introducing ternary operators is complex because they require new syntax
rules as well as two operators not used anywhere, so they end up consuming
more tokens combinations, and we would need to add a handful more in the
name of extensibility.

Therefore, the Elixir-y thing to do is to rely on existing constructs and
use the same constructs to solve different problems. That’s why defining a
function is syntactically the same as a conditional. That’s why cond and
case and try follow a similar structure. This means everyone can create
similar constructs too. The goal is to do more with less.

> 1. "It's too cryptic" — I just need to point to the `&(&1)` type shortcut
to show precedence for smaller syntax sugar in elixir.

Note the existence of “cryptic syntax” is not an argument for adding more
cryptic syntax. I am not saying that any of these are cryptic, just that
the counter-argument does not hold :)

On Fri, Jan 26, 2024 at 23:07 Brandon Gillespie <bran...@cold.org> wrote:

> I'm sure there's a reason, so I figured I'd ask why there isn't a ternary
> expression?
>
> It seems like ternary would help with the functional paradigm of Elixir.
>
> I realize one can use the `if...` as a construct, but it just isn't very
> readable (imho of course).
>
> Consider:
>
> some_function(
>   log_id,
>   if(type == :before, do: action.order, else: action.order + 1)
> )
>
> vs:
>
> some_function(log_id, type == :before ? action.order : action.order + 1)
>
> Invariably when I come upon a use case for a single-line if, I end up
> never liking its readability. So instead of the above, I end up with:
>
> new_order = if type == :before, do: action.order, else: action.order + 1
> some_function(log_id, new_order)
>
> While this works, it's also not great, because I try to avoid assignments
> wherever possible (being a functional language and all).
>
> Anyway, just a question more than anything. Thanks!
>
> -Brandon
>
>
> A few other things to address suggestions I know may come:
>
> 1. "It's too cryptic" — I just need to point to the `&(&1)` type shortcut
> to show precedence for smaller syntax sugar in elixir.
>
> 2. Just make a `ternary()` function — but I don't think it really helps
> any with the visual readability, and in fact is probably LESS so since it's
> a less known function/name in the general programming world, where ternary
> ?: is pretty ubiquitous.
>
> 3. use pipes in the above last example — not what I want, because that
> requires re-ordering the function's arguments. That's a bizarre requirement
> just because we can't handle ternary's.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/112d8d35-c65e-4d8d-a221-5707c211f26b%40cold.org
> <https://groups.google.com/d/msgid/elixir-lang-core/112d8d35-c65e-4d8d-a221-5707c211f26b%40cold.org?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4LM1db%3D8-6NfsuyeAHX4P0UykaW-rWNzAKBu6fyWnPg%3DA%40mail.gmail.com.

Reply via email to