Live data from Hacker News

Swift is like Kotlin

nilhcem.com

21–30 of 364 posts

Re: Swift is like Kotlin

#21
I feel like you could about as well write an article about how Go or Scala or TypeScript is like Kotlin. They all have some cases where they look similar or have some similar constructs borrowed from other sightly-less-recent languages. It doesn't seem like a very interesting or deep similarity.

If you want to compare them, going over the differences would be a lot more illuminating.

Re: Swift is like Kotlin

#23

Could someone tell me the meaning of _ in this Swift code: func greet(_ name: String,_ day: String) -> String { return "Hello \(name), today is \(day)." } greet("Bob", "Tuesday") In me prior experience usually _ denoted an unused parameter/variable. Also: I find the string interpolation syntax ugly... The syntax choices in Swift are curious, but not the most aesthetical/practical in my opinion.

Swift has both external and internal parameter names for functions. This is to make API design more readable, easier to parse through.

For example, you can have a function like so:

    func greet(with greeting: String, to personName: String) {
        print("\(greeting), \(personName)!")
    }
In the above code, "with" and "to" are external names, and are only available when calling the function but are not available inside the function itself. So you would call this function like so:

    greet(with: "Hello", to: "Bob")

Now if you want to exclude the external name to call the functions, you use the "_" syntax. So you're right that it denotes an unused parameter. In this case, it's an unused external parameter name.

Re: Swift is like Kotlin

#24
There does seem to be broad agreement across a range of typed languages -- TypeScript, Swift, Kotlin, ES6 + Flow -- about notations for classes, control flow and data structure declaration.

But true convergence remains far away...

Re: Swift is like Kotlin

#25
post #14
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...

it's always confusing, in languages with ranges, whether the range includes the second boundary element or not. it's purely a matter of convention, after all. ruby has a..b and a...b, but even after years of using the language i have to look up which is which. a.. perfect notation - once you know the language handles both cases, a.. as for the "count - 1", it's not too bad because i find "inclusive" more intuitive th…

Why is it a..b instead of the logical a..=b or a..<=b?

Re: Swift is like Kotlin

#26

Could someone tell me the meaning of _ in this Swift code: func greet(_ name: String,_ day: String) -> String { return "Hello \(name), today is \(day)." } greet("Bob", "Tuesday") In me prior experience usually _ denoted an unused parameter/variable. Also: I find the string interpolation syntax ugly... The syntax choices in Swift are curious, but not the most aesthetical/practical in my opinion.

You can name parameters externally and externally explicitly.

    func sayHi(to personName: String) {
        print("Hi \(personName)")
    }
    sayHi(to: "Bob") // Hi Bob 
Similarly, using just one label in the function declaration means that they have the same name externally as well as internally.

In this case the example is saying that they don't want any external labels.

edit: damnit, beaten

Re: Swift is like Kotlin

#27
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.

In Kotlin it's much more common to write the following, which is exclusive:

   count.forEach {
      // do things
   }

Re: Swift is like Kotlin

#28
post #2

0..<count is a lovely little piece of language design

An alternative approach is Guava's Range class:

Range.closed(1,5) == [1,5]

Range.open(1,5) == (1,5)

Range.openClosed(1,5) == (1,5]

Range.closedOpen(1,5) == [1,5)

Range.greaterThan(1) == (1,infinity)

Range.atLeast(1) == [1,infinity)

That class always strikes me as having high power-to-weight. It has methods like encloses(anotherRange), contains(aValue), and others.

https://google.github.io/guava/releases/19.0/api/docs/com/go...

Re: Swift is like Kotlin

#29
Pretty good list. I don't think the tuple comparison is accurate though, I thought a data class is basically a value type (like Swift's struct) except with some stuff like equals, hashcode, and toString calculated for you.

Minor nit: the sort example needlessly uses two lines for the Swift version.

Re: Swift is like Kotlin

#30
post #2

0..<count is a lovely little piece of language design

I can't tell whether this is sarcasm or not. What does the '<' mean in this case? I'm assuming an exclusive upper bound.

Read it as "For 0 to count" basically. "0... (spread operator) less than 0. It seems confusing to me as well at first read but I'm used to writing for loops by hand uphill both ways.
Post reply on HN