Hacker News new | past | comments | ask | show | jobs | submit login

Some considerations on the syntax (Swift first, Kotlin last in each pair)

    bad: \(apples + oranges) # using \ is looking for troubles 
    good: ${apples + oranges}

    good: label + String(width) # even Ruby requires a .to_s here
    bad: label + width # surprises will follow

    good: ["Anna", "Alex", "Brian", "Jack"]
    super bad: arrayOf("Anna", "Alex", "Brian", "Jack") # make the long form optional

    good: for index in 1...5 {
    bad: for (index in 1..5) { # the useless ()

    bad: extension Double { # why do we need to be so explicit?
        var km: Double { return self * 1_000.0 }
    good: val Double.km: Double get() = this * 1000



> # using \ is looking for troubles

Are you referring to escaping and other special sequences (e.g. unicode codes)? Though it is odd-looking I actually find it rather smart that they decided to reuse an existing mechanism for that: aside from delimiters the only magical character in a string is \ rather than have e.g. both \ and $.

> # the useless ()

My beef is more with 1..5 being inclusive and there apparently being no exclusive range.

> # why do we need to be so explicit?

Why would you want syntax which is harder to parse, more magical, and has to be repeated for every addition, instead of an easily marked `extension` block which neatly parallels `struct` or `class` ones?


About \, yes it's the possible clash with a real quote. More an inconvenience to the reader that a source of bugs, but also that.

About ranges, Ruby has both .. and ... and after 12 years I don't remember which is what. Maybe an inclusive one is enough and 1..(n-1)

About syntax in general, it should be easy for developers no matter how hard it is for the compiler/interpreter to parse it. I don't like to have to type useless characters. Hence the () around if conditions and this extension thing. What else can it be? I'm defining a method of a class, no need for a special block. But I don't understand your "has to be repeated for every addition" so maybe I'm missing something important here.


> About \, yes it's the possible clash with a real quote.

Clash with a real quote? As in you paste random text from wherever and it turns out evaluated? You've got the exact same issue in Kotlin.

> About ranges, Ruby has both .. and ... and after 12 years I don't remember which is what. Maybe an inclusive one is enough and 1..(n-1)

That's why I like that Swift uses "a..<b" for exclusive, you clearly see the top-bound being excluded from the range. It's readable and smart design.

> Maybe an inclusive one is enough and 1..(n-1)

I have never missed inclusive ranges in languages with only exclusive ranges (e.g. Python), the opposite would not be true.

> About syntax in general, it should be easy for developers no matter how hard it is for the compiler/interpreter to parse it

That way lie Perl and C++ and undecidable syntaxes, I'm very much opposed to that.

> I don't like to have to type useless characters.

I don't see what's useless about saying what you're asking for. You define a struct your use a struct block, you define a protocol you use a a protocol block, you define an extension you use an extension block. It's readable and regular.

> But I don't understand your "has to be repeated for every addition" so maybe I'm missing something important here.

If you're adding more than one item in Kotlin you have to repeat the receiver e.g.

    val Double.km = (thing)
    val Double.miles = (thing)
    val Double…
in Swift you just wrap it all in an extension block:

    extension Double {
        val km = (thing)
        val miles = (thing)
        val ...
    }


"When you see that third dot, imagine opening the accordion slightly. Just enough to let one note from its chamber. The note is that end value. We’ll let the sky eat it." - http://poignant.guide/book/chapter-3.html#section2


1..5 syntax has been deprecated for about 2 years, now you have 1...5 which is inclusive and 1..<5 which excludes the last element.


does it work with expressions? foo()...bar()? I can count on no fingers the amount of times I've hardcoded the length of a loop in production code.


Yes it does. (both ... and ..< can be used with expressions that return an int)


I was talking about Kotlin here.


Ah, I read your comment badly, sorry.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: