Syntax suggestion for "when..."

Coming from a predominantly (but not exclusively) Delphi/Pascal background for the last two decades, I took to the Kotlin syntax like a duck to water, and I’m loving’ it ! I’ve also dabbled in C, C++, VB, C# and Java, etc. during that time. With the exception of Delphi Pascal’s ubiquitous and clunky “begin…end” blocks instead of curly braces (a long-standing bugbear of mine), the Delphi/Pascal syntax is very concise, elegant and expressive, just like Kotlin’s, as are Borland’s (–>CodeProject, -->Embarcadero’s) object oriented extensions to ANSI Pascal. So, a) I’m not surprised to see Pascal-like syntax in Kotlin, and b) keywords like “is, as, in, var”, and variable and function types specified after a colon (:), etc.etc.etc. are very Delphi-ish/Pascal-ish/familiar to me.

The only glaring inconsistency (in my very humble opinion) in the Kotlin syntax is the “when…” case/switch construct. Syntactically it just looks all wrong using -> for each case. When compared to other languages’ use of ->, nothing is being mapped, per se, and it’s not defining a lambda expression.

Having borrowed so much from Pascal, it would be much neater to use the good old colon again,
e.g.

when (i) {
1: println(“something”)
2: println(“something else”)
else println(“oops !”)
}

or even…

else: println(“oops !”)

This is, to all intents and purposes, identical to C, Delphi Pascal and Java and there’s nothing glaringly wrong with them that needed improving, with the exception of C’s constant repetition of the “case” keyword. And certainly "else -> println(“wtf ?”) doesn’t at all seem in keeping with Kotlin’s philosophy of not being unneccessarily verbose. In the “else” clause, -> seems completely redundant to me, syntactically- and token-wise.

Kotlin could even have the World’s first “when…then” construct:

when (i) {
1 then println(“maybe”)
2 then println(“just my two-penneth”)
else println("maybe a bit COBOL-y though ? :stuck_out_tongue: ")
}

Or have the colon syntax (when… 1: etc) and “when…then” as optional interchangable alternatives of each other. But definately not -> !

Is it too late to change the “when” syntax ?..please ??? :smiley:

Just needed to get that off my chest.
Please don’t all jump down my throat at once :smiley:

Baz.

I like the idea, although I suspect it’s too late to do much about it. The old syntax won’t change. Maybe in a 2.0 release, but I wouldn’t get my hopes up. There is a tiny possibility for an alternative syntax, but I don’t think it’s likely.
That said, I agree that -> is clunky and doesn’t really fit in when-expressions and I would much prefer : as well.

1 Like

I would prefer : as well. But I don’t see it coming. Still curious why they’ve chosen -> though.

1 Like