I apologize in advance is this is the wrong place, I’m brand new to this forum and on-and-off new to Scala as a whole.
My question is in regards to ternary operators in general and their related operator types, such as null coalescing.
Personally, I find them a joy to write and read when written appropriately (read: limited nesting). In the documentation on scala-lang.org, I found that the reason for Scala’s lack of these operators is cited as readability. While I most certainly understand this perspective I do have several questions regarding it/other perspectives I was considering. I was hoping that someone might be able to answer or rebut them?
-
The first is more of an abstract question. It is impossible to force developers to write purely readable code. No matter what tools the language gives them to help enforce readability, engineers will find a way to make it over-complicated and unreadable. Are there any deeper reasons that I am missing?
-
Let’s take a look at the following (admittedly dumb) example, assuming there is a function
findPerson()
that returnsOption[Person]
from a database somewhere.Scala Today:
val person = findPerson().getOrElse(null) val name = if (person != null) person.name else "No person found"
Scala with Ternary:
val person = findPerson().getOrElse(null) val name = person != null ? person.name : "No person found"
Scala with Null-Coalescing and Falsey-Null:
val person = findPerson().getOrElse(null) val name = person.name ?? "No Person found"
Are any of these anti-Scala? If so, why?
To clarify, I’m honestly asking this question to learn how to better write “Scala-like Scala”. One of the most fun parts of learning a language to me is learning the “-isms” of the language.
I love the language and this community, so thank you to everyone who has worked so hard to make this language what it is today!
Edit: I am aware of things like this, just sort of wondering why it is not in the language itself.