Live data from Hacker News

Swift is like Kotlin

nilhcem.com

251–260 of 364 posts

Re: Swift is like Kotlin

#251
post #220

Earlier quoted context omitted.

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)

Re: Swift is like Kotlin

#252

Does anyone else think the Kotlin syntax for array literals is super ugly: val shoppingList = arrayOf("catfish", "water", "tulips", "blue paint") What is wrong with square bracket syntax? (rant over). EDIT: I suppose it's consistent with other syntax e.g. listOf() etc.

I agree, it leaves a bad taste in the mouth. It's even worse than old PHP's array(). I'll pick short forms all the time: lists with (), dictionaries/hashes with {}.

Python does this too, and I dislike it slightly. I'd prefer uniform syntax for collections, such as in Lua or golang.

Re: Swift is like Kotlin

#253

Earlier quoted context omitted.

Would you work in a programming language called Hitler? Any multinational company needs to do their research before deciding on a name.

Equating a programming language named after a Russian island to Hitler is a real stretch, unless you think that everything Russian is like Hitler. It's just an island and a language named after it.

What about Brainfuck?

Re: Swift is like Kotlin

#254

Earlier quoted context omitted.

Whereas it's completely reasonable to object against naming something after an insect?

Yes, it is pretty normal to have phobia of e.g. insects. If you have phobia of Russia on the other hand, you might want to get checked.

The Cold War would like a word.

Re: Swift is like Kotlin

#255

Does anyone else think the Kotlin syntax for array literals is super ugly: val shoppingList = arrayOf("catfish", "water", "tulips", "blue paint") What is wrong with square bracket syntax? (rant over). EDIT: I suppose it's consistent with other syntax e.g. listOf() etc.

Plain Java has similar function too: https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.h...

Re: Swift is like Kotlin

#256
post #220

Earlier quoted context omitted.

> # 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 bein…

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.

I was talking about Kotlin here.

Re: Swift is like Kotlin

#257

Earlier quoted context omitted.

> Does anyone else think the Kotlin syntax for array literals is super ugly: Well yeah, except it's really that Kotlin does not have array literals at all, arrayOf is just a variable-arity function.

I guess they think they are cool for being able to provide this feature through a library function rather than a language construct, but someone should tell them they really need to add some syntactic sugar that transparently invokes arrayOf while looking nicer.

It's worth nothing that Swift literals are mostly library constructs as well. Any type conforming to ArrayLiteralConvertible can be instantiated using [...] syntax, for example. All other literal types (integers, floats, strings, dictionaries, etc.) have their own corresponding protocols you can implement. The only thing that's special for types like Array, Int, or String is that the compiler will automatically infer those types for a literal if there's nothing that indicates otherwise.

For example:

  let x = [1, 2, 3] // produces Array
  let y: MyType = [1, 2, 3] // produces MyType
  f([1, 2, 3]) // produces whatever type f() takes

Re: Swift is like Kotlin

#258
post #86

Earlier quoted context omitted.

> [Kotlin Native] features automatic reference counting with a cycle collector on top, but what the final memory management solution(s) will look like is unknown at this point. https://blog.jetbrains.com/kotlin/2017/04/kotlinnative-tech-...

So it's not ARC (in the Swift sense), it's just RC + cycle breaker.

What do you mean by "just"? ARC is just RC done by the compiler. Kotlin Native is ARC + cycle collector.

Re: Swift is like Kotlin

#259

Earlier quoted context omitted.

> # 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 bein…

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…

> 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..> 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 ...
    }

Re: Swift is like Kotlin

#260
post #40
post #6

I like swift's conditionals without brackets, OTOH the syntax for string interpolation is ugly

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.

I don't see why that matters if they're always balanced.

  "blah blah \(f(g(42)))"
Seems fine to me.
Post reply on HN