OT: Swift is now open source

Michel Fortin via Digitalmars-d digitalmars-d at puremagic.com
Sat Dec 5 18:02:25 PST 2015


On 2015-12-04 07:51:32 +0000, Jacob Carlborg <doob at me.com> said:

> On 2015-12-03 20:10, Steven Schveighoffer wrote:
> 
>> The truth is, swift is orders of magnitude better than Objective C.
>> 
>> I have gotten used to the nullable API, though it sometimes seems more
>> clunky than useful.
> 
> I find it very clunky as well. Sometimes it's too strict. I was a bit 
> surprised when I noticed that converting from an integer to an enum 
> returned an optional.

It's about preserving the invariants of the enum type, namely that 
it'll always have one of the allowed values. Append ! after the 
expression to unwrap the optional if you don't care about what happens 
when the value is invalid.


>> Apple's API is still rather verbose and hard to discover, but that is 
>> not swift's fault.
> 
> They could have gone the D route by separating the method name from the 
> selector:
> 
> extern(Objective-C) class Foo
> {
>      void bar() @selector("thisIsMyReallyLongSelector:withAnotherSelector:");
> }

You can do that in Swift too with @objc(some:selector:). And for Swift 
3 they do plan to give Swift-specific names to pretty much all methods 
in the Apple frameworks.
https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md


And 

>> the lack of semi-colons has poisoned me from writing syntactically
>> valid lines in D :)
>> 
>> I miss D's algorithms and range API when working with swift. A lot. I've
>> tried to use their sequence API, but it's very confusing.
> 
> I have not used it much, but I think it it's quite alright. But it's 
> ridicule complicated to slice a string in Swift compared to D. One 
> needs to pass in a range of a specific index type.
> 
> var str = "Hello, playground"
> str.substringWithRange(Range<String.Index>(start: 
> str.startIndex.advancedBy(2), end: str.endIndex.advancedBy(-1))) 
> //"llo, playgroun"

You can be less verbose if you want:

	let str = "Hello, playground"
	str[str.startIndex.advancedBy(2) ..< str.endIndex.advancedBy(-1)]

Also note that those special index types are actually iterators. You're 
decoding characters (grapheme clusters) as you advance those indexes.


> [...] Swift doesn't even have "find" (as far as I can see) the use of 
> optional types would be perfect here.

It's called indexOf. (Remember, the index type is an iterator.) It does 
return an optional. It will work for any type conforming to the 
ContainerType protocol where Element conforms to Equatable. Like this:

	let str = "Hello, playground"
	let start = str.unicodeScalars.indexOf("p")!
	let end = str.unicodeScalars.indexOf("g")!
	str.unicodeScalars[start ..< end] // "play"
	str.unicodeScalars[start ... end] // "playg"


-- 
Michel Fortin
michel.fortin at michelf.ca
http://michelf.ca



More information about the Digitalmars-d mailing list