Which is immaterial. The question is Kotlin, where ? is 100% associated with nullability.
And I see no profit in having that restriction.
Every programmer reuse his knowledge of natural languages, math notation, other programming languages.
Say ?
will always be associated only with nulls in kotlin. But not in programmers mind.
What would be the point in supporting that made up rule?
Simpler learning? I doubt it.
What new information will you give to other person, who donât know kotlin, when you tell him that rule? None. Just some curious fact, an anecdote.
I did not say it as a restriction but a statement of fact. ? Already is used in the Kotlin language and 100% of those uses involve nullability. In Java ? is only used as the conditional operator. Adding a non-nullable use for it leads to confusion.
Couple of other points:
Your natural language example was used to express whether Mark was going to be there. That is actually what Kotlin uses it for, to express that something may not be there (may be null).
And in natural language when we want to express 2 alternatives based on whether something is true or not we do something like, âif it is raining then drive to work else bike to workâ. Note that no question marks are used to express that. It is not natural to instead write, âit is raining? drive to work: bike to workâ. Kotlin lets you express it in a way that is closer to natural language, âif(itIsRaining) driveToWork() else bike to work()â.
Your argument that ? : is natural language is not supportable.
I think you considered english only
Well programming lan[quote=âguai, post:156, topic:2116â]
I think you considered english only
[/quote]
Programming languages tend to be based on english and this has a few advantages. Every international software company has to decide on one language to use and this is most of the time english. Mixing languages in code getâs confusing. I once had to deal with a smalltalk program where everything was named in german. I speak german so this is not a problem for me but it still looks strange.
There are many more non-international software companies than international ones. Mixing languages can carry additional information, like we use national language for busyness logic and english for core code. When there are several teams of programmers its better to use national terminology than several variants of translation or (god forbid) transliteration.
Besides, programmers still think in their native languages, math notation, other programming languages that they learned before kotlin, many of which have ternary.
BTW you can mix programming languages in one project too. In case of kotlin it most certainly be java or groovy, both of which have ternary.
For that reason I donât really like kotlinâs types on the wrong side, but thats another story
I donât mind using multiple programming languages in a project. The problem I have if I have to switch languages mid expression multiple times.
Also maybe I did not explain my point properly. Programming languages are based on english as most people using them can speak it. Adding features to Kotlin based on languages most people donât speak will just confuse them.
Iâm really glad that Kotlin doesnât have the ternary operator since it has been a source of frustration in Java due to:
- Nested ternaries are difficult to read and easy to misinterpret
- Single ternary that doesnât fit on one line is difficult to read
- Accidentally inverting the ternary by putting the false case first (I have seen this)
- Thereâs always the odd developer that likes to abuse the ternary operator and makes it difficult for the rest of the team
I use this as a selling point when promoting Kotlin (in addition to the larger benefits)
Me too! Iâm glad others are selling Kotlin on this. It surprises me that people still prefer the ternary operator over if-expressions after theyâve tried them. Would those people also prefer Pythonâs ternary operator, âa if condition else b
â?
I feel this topic has been well summarized several times by multiple people over the last year. Should this topic be closed?
And you ignored everything I said that refutes your claim to make a nonsensical reply.
Kotlin keywords are based on English so yes I of course stuck with English just as you did. Are you claiming there is some non-English natural language where one routinely says âit is raining? drive to work: bike to workâ
Iâm claiming that ternary is quite familiar concept for most programmers either from other langs from largely grown C-family or natural languages and was never broken. Unless of course deep nesting of ternary. Which is also true for if
and any other statement since 5-6 levels of nested if
s is not so readable too.
But thats ok cause IDE can fix it in a moment and replace ternary with if
s.
And lack of ternary is especially annoying on multi-language projects.
Iâm also claiming that sticking to the rule â?
always relate to nullâ adds nothing to kotlin.
as actual proof of your words âIâm claiming that ternary is quite familiar concept for most programmersâ is swift lang: swift uses ? for optional chaining(Optional Chaining â The Swift Programming Language (Swift 5.7)) like this:
if let roomCount = john.residence?.numberOfRooms {}
and swift uses ? for ternary operator.
But, need kotlin designers to add ternary, they would already add it in kotlin.
Actually that was not the argument you were making. That is the ONLY valid argument for ternary operator, but is not a particularly compelling one. There is plenty familiar to Java programmers that were jettisoned in Koitlin for good reason.
For those not coming from a C-based language it is very unfamiliar. if-else is familiar to both.
And we have to ask why did it exist in those C-derivative languages? Because it filled an important role because if was a statement in those languages and not an expression. Since Kotlin eliminated that problem and if is an expression there is no need to use the confusing hack of the ternary operator.
That was the actual claim you did make but did not adequately support and is not valid. Unless you can produce a natural language that uses ? : as a ternary expression then your claim is invalid.
Actually it was always broken and a bad hack. See: ternary operator considered harmful - Google Search
Adding the ternary operator would add nothing to Kotlin as it would be a redundant construct that is non-obvious to those unfamiliar with it. It would detract from Kotlin in that it adds confusion by giving an alternate meaning to ?.
And once again they did that because, like Java, C and other languages, they made âifâ a STATEMENT not an EXPRESSION. When if is an expression as it is in Kotlin then the ternary operator would just be a an ugly, non-obvious alternative to the much more clear if-expression.
Iâve created a topic to discuss closing this topic and the companion object topic: Please close the mega topics (ternary and companion objects)
Then why language designers adding it over and over again? Swift, groovy, js, C#. I bet there are many more
I never claimed that there are natural languages with that exact syntax, it was you imagination. I claimed that in natural languages there are similar constructs, and anyone can recognize this constructs in ternary.
Why ruin all the fun?
And who would be that people writing on jvm-based lang, stated to be better java, stated to be targeting java developers, and unfamiliar with ternary?
Anyway its no big deal to grasp it
Reasons to not add the ternary operator:
- It doesnât add any new functionality
- If-expressions work okay as they are now
- It can lead to unclear code when chaining ternaries
- It can lead to unclear code when using long expressions
- It is difficult to understand for those new to programming
- It clashes with Nullabilityâs use of the question mark
- It clashes with potential uses of the colon
- The minus 100 point rule
Reasons to add the ternary operator:
- Itâs more concise (shorter character-wise)
- Itâs expected by users coming from other languages
- Itâs understood by users coming from other languages
- It does not clash with Nullabilityâs question mark (see other languages)
- It doesnât limit other future uses of the colon
Reasons to end the fun:
- Fun is fun
- Fun is done
- This topic: https://discuss.kotlinlang.org/t/please-close-the-mega-topics-ternary-and-companion-objects/
- Nothing would be as fun as seeing these two topics closed
Because they failed to fix the fundamental problem that if was a statement rather than an expression. So they just fell into copying the ugly alternative rather than fix the problem.
This is what you said:
And yet you provided no examples of this. You provided an example that agrees with Kotlinâs use of ? for nullability. I provided an example where natural language uses an if-else style of syntax.
But I agree with arocnies, stop this thread.