Live data from Hacker News

Swift is like Kotlin

nilhcem.com

31–40 of 364 posts

Re: Swift is like Kotlin

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

Is there any language where the meaning isn't immediately clear? I haven't seen a language that didn't differentiate between '<=' and '<'. The syntax of just '..' is a little unclear at first however, but it is simply a requirement of learning the language and the idioms

Re: Swift is like Kotlin

#32
post #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…

Should the second "closed" be "closed Open" (with the corresponding change to the rest of the line)?

Re: Swift is like Kotlin

#33

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.

I dunno, I think Swift's string interpolation is clever. Using the special-char backslash for opening string interpolation is a neat idea. Also keeps the number of special chars in in strings to a minimum.

Re: Swift is like Kotlin

#34
post #3
post #2

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

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

In the early beta release of Swift 0.x, they used x..y for half-open ranges and x...y for closed ranges. This confused people (especially since Ruby already used the same operators but the other way around), so they changed the half-open operator to x..<y.

Re: Swift is like Kotlin

#35
post #5

Earlier quoted context omitted.

When I see "0..n" I wonder whether the range is inclusive or exclusive of n. When I see "0..<n" I know the range is exclusive of n.

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.

Re: Swift is like Kotlin

#36
post #16
post #2

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

Indeed a clear syntax. OTOH, a "loop .. for .. {upto, below} count" leaves less questions in a casual code review :)

I prefer explicit words over symbols, like in Scala:

scala> 1 to 3

scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)

scala> 1 until 3

scala.collection.immutable.Range = Range(1, 2)

Re: Swift is like Kotlin

#37
post #32
post #28

Earlier quoted context omitted.

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…

Should the second "closed" be "closed Open" (with the corresponding change to the rest of the line)?

Fixed, thanks.

Re: Swift is like Kotlin

#38
post #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…

What does a Guava two-Integer-element array literal look like?

Re: Swift is like Kotlin

#39

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

Is this broad agreement due to theoretical alignment, or a deliberate attempt to increase programmer adoption?

If true convergence happened, there wouldn't be a need for different languages. :-)

Re: Swift is like Kotlin

#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.
Post reply on HN