Live data from Hacker News

Swift is like Kotlin

nilhcem.com

111–120 of 364 posts

Re: Swift is like Kotlin

#111
post #40

Earlier quoted context omitted.

Ugh, I know it's something that literally every programmer on earth can and often does bikeshed, but Swift just got string interpolation syntax wrong . Parentheses are something that are often part of the interpolated expression... they shouldn't be part of the interpolation syntax itself.

It's so pretty but that's really disappointing. That puts a damper on even trying it but to be fair it could be a lot worse.

>That puts a damper on even trying it

Hyperbole much?

Re: Swift is like Kotlin

#112

One has structures, ARC, proper extensions, good protocols/interfaces, flexible enums, and runs natively. The other one doesn't. I think I'll choose the former.

I'll take good IDE support over any of the things you mention above. Seeing how my colleagues refactor in XCode makes me cringe every time... Kotlin support in IntelliJ is almost as good as Java.

Re: Swift is like Kotlin

#113
post #97

Earlier quoted context omitted.

Kotlin has a REPL

So does Swift ( https://developer.apple.com/swift/blog/?id=18 ) ( https://repl.it/site/blog/swift )

You've missed the point. He is saying that to get a hello world going in Kotlin you don't need to write a func if you are in the context of the REPL.

Re: Swift is like Kotlin

#114
* A feature I like about kotlin are the receivers, with which you can write type-safe builders: https://kotlinlang.org/docs/reference/type-safe-builders.htm...

* Multiple receivers are possible enabling extension methods like this:

    class EnglishToGermanDictionary {

       val translations = mapOf(
          "dog" to "Hund"
       )

       /* extension method for string in the context of dictionary */
       val String.inGerman get() = 
          translations[this@String]
    }

    /* usage */
    with (EnglishToGermanDictionary()) {
       val word = "dog"
       println("The translation of $word is ${word.inGerman}")
    }
 
    "cat".inGerman ---> compile error, method not found

* Built-in Singletons are also very convenient. Swap the word `class` for `object` and you get a singleton.

* The article left out lambdas. The function:

    fun greet(name: String, day: String): String {
        return "Hello $name, today is $day."
    }

    Can also be written as

    val greet = {name, day -> "Hello $name, today is $day."}

* The compiler is really smart in infering types. Extension values don't need type-specification, e.g.:

    val Double.km get() = this * 1000

* "when" in Kotlin doesn't need an argument, you can write arbitray rule-sets with it, with correctly inferred conditions:

    val myVar = when {
        stack.isEmpty() -> true
        x == null -> false
        currentIdx >= a && obj is MyType -> true   /* here x has been inferred to be not null from the previous branch! */
        else -> when {
            user.isPresent() -> calculateSomething()
            else -> throw IllegalStateException()
        }
    }
* sealed classes allow you to limit inheritance and get error when you miss something in a when

    /* you actually don't have to nest the classes this way, but you can */

    sealed class Expr {
       sealed class IntExpr : Expr {
          class Plus(val left: Int, val right: Int): IntExpr
          class Minus(val left: Int, val right: Int): IntExpr
       }

       sealed class StringExpr : Expr {
          class Substring(val str: String, val start: Int, val end: Int): StringExpr
       }
    }
      
    /* Usage: */
    val x: Expr
    when (x) {
       is Substring -> ...
       is Plus -> x.left + x.right

       /* compile error, we forgot to handle "Minus" from above */
    }

Re: Swift is like Kotlin

#115
post #3

Earlier quoted context omitted.

What do you like about it? I was actually thinking the opposite... EDIT: Genuinely curious, this is not meant to troll or start a war. I like the more explicit feeling `count-1`. I'd use either...

How is the inclusive range operator implemented in Kotlin? Something like `for (i in 0..count)` needs to iterate count + 1 times, which may overflow the size of count's type - annoying to implement.

>needs to iterate count + 1 times, which may overflow the size of count's type - annoying to implement

How's that different from:

  for (int i=0; i
in e.g. C99?

Re: Swift is like Kotlin

#116
post #35

Earlier quoted context omitted.

Yes, but the corresponding design decision would be "0..=n". Swift's "0...n" is a bit less obvious. Also, exclusivity is almost always what you want, so "0..n" should just default to exclusivity and "0...n" could be inclusive.

As an alternative opinion, I personally hate when ranges are exclusive by default. In addition to it feeling somewhat unintuitive to me that I need to use range(0, 6) to include 5, it also makes downward ranges quite awkward, e.g. needing range(5, -1) to count from 5 to 0.

On the other hand, it's easier to determine the number of items in a range if it is exclusive; range(n, m) contains abs(m - n) items.

Also, inclusive doesn't allow you to specify empty ranges. That may mean having to add if statements to your code, making it uglier.

I would prefer using notations [m,n) and [m,n], even though that uses a notation used in mathematics for a set to specify a sequence.

Of course, one would want (m,n) and (m,n], too, then, but that first one probably would make parsing your language difficult.

Re: Swift is like Kotlin

#117

One has structures, ARC, proper extensions, good protocols/interfaces, flexible enums, and runs natively. The other one doesn't. I think I'll choose the former.

I'll take good IDE support over any of the things you mention above. Seeing how my colleagues refactor in XCode makes me cringe every time... Kotlin support in IntelliJ is almost as good as Java.

So, when is JetBrains going to have a decent documentation viewer?

Re: Swift is like Kotlin

#118
post #89

Earlier quoted context omitted.

If they open source it, maybe someone should fork it called Kiev.

Ironically, the name "Kiev" would be no less offensive becuase this spelling of the name implies that this is a Russian city, not a Ukrainian one. The official and correct spelling is "Kyiv", see http://kyiv.of-cour.se/ for explanations why.

I'm pretty offended that you call my home Великобританія too.

Re: Swift is like Kotlin

#119
post #95
post #89

Earlier quoted context omitted.

Ironically, the name "Kiev" would be no less offensive becuase this spelling of the name implies that this is a Russian city, not a Ukrainian one. The official and correct spelling is "Kyiv", see http://kyiv.of-cour.se/ for explanations why.

Ironically, the name "Kyiv" would also be no less offensive since it is written in Latin characters non native to Ukraine instead of the Ukrainian alphabet, which is a derivative of the Cyrillic alphabet. The official and correct spelling is "Київ"

Totally not. Many newly-installed signs in Kyiv are bilingual, in Ukrainian and in English. We have official rules for transliterating Cyrillic into Latin which the government uses.
Post reply on HN