Re: [swift-users] Simplify chained calls

2016-06-28 Thread Dan Loewenherz via swift-users
I’m not sure if you wanted to stick with the pure functional approach, but here’s an alternative that uses Range to take care of most of the work. func selectionSort(_ array: [Int]) -> [Int] { guard let minValue = array.min(), let index = array.index(of: minValue) else { return []

Re: [swift-users] Simplify chained calls

2016-06-28 Thread Dan Loewenherz via swift-users
On Tue, Jun 28, 2016 at 9:58 PM, Erica Sadun wrote: > Most everyone is doing two passes, one to get the minimum value, another > to get its index. > I aesthetically prefer using enumerate to do both at once. > > -- E > Makes sense. Here’s a revision. It’s not as simple, but it does just one pass

Re: [swift-users] C API returns null but optional thinks it's set anyway

2016-07-01 Thread Dan Loewenherz via swift-users
What’s the Swift type signature on GDALOpen? My guess is that it’s returning a UnsafePointer or UnsafeMutablePointer type. Swift doesn’t currently handle nullability for C pointers (hence the “Unsafe” prefix). You’ll need to check the value at the pointer’s location manually. Dan On Fri, Jul 1, 2

Re: [swift-users] Why can't Swift instance methods call class methods without qualification?

2016-07-01 Thread Dan Loewenherz via swift-users
On Fri, Jul 1, 2016 at 11:58 AM, Jens Alfke via swift-users wrote: > > > > On Jul 1, 2016, at 9:38 AM, zh ao wrote: > > > > Swift forces you to use class name to alert you on the fact that static > > variables and methods (may) affect the other instances of the class as > > static variables are

Re: [swift-users] Switch based on let

2016-07-08 Thread Dan Loewenherz via swift-users
To my knowledge, you can’t do exactly what you’re trying to do, but this is close: for subclassObject in objects { switch subclassObject.self { case is Subclass1: doSomethingWith(subclassObject as! Subclass1) case is Subclass2: doSomethingWith(subclassObject as! S

Re: [swift-users] Switch based on let

2016-07-08 Thread Dan Loewenherz via swift-users
. > > > > I wanted to be sure I wasn't missing something in my syntax (nor some > > obvious-to-others reason this isn't supported) before going to swift > > evolution. > > There is a bug filed to improve this error message: > https://bugs.swift.org/browse

Re: [swift-users] How do you use protocol types?

2016-07-13 Thread Dan Loewenherz via swift-users
Unless you need to abstract what the Factory class does, I would eliminate AProto. class Factory { func constructInstance(_ t: T.Type) -> T { return t.init() } } If you truly want to genericize Factory, you can do the following: class Factory: AProto { typealias Elemen

Re: [swift-users] Protocol with instance var that's set on construction, otherwise read-only

2016-08-03 Thread Dan Loewenherz via swift-users
On Wed, Aug 3, 2016 at 3:51 AM, Rick Mann via swift-users < swift-users@swift.org> wrote: > > > On Aug 2, 2016, at 19:06 , Jordan Rose wrote: > > > > I don’t think it makes sense to do this. A protocol cannot control how a > particular property is implemented (stored or computed), and any conform

Re: [swift-users] Protocol with instance var that's set on construction, otherwise read-only

2016-08-09 Thread Dan Loewenherz via swift-users
On Mon, Aug 8, 2016 at 6:16 PM, Rick Mann wrote: > > > On Aug 3, 2016, at 03:23 , Dan Loewenherz wrote: > > > > On Wed, Aug 3, 2016 at 3:51 AM, Rick Mann via swift-users < > swift-users@swift.org> wrote: > > > > > On Aug 2, 2016, at 19:06 , Jordan Rose wrote: > > > > > > I don’t think it makes

Re: [swift-users] How to extend a generic type with a Bool constraint?

2016-08-31 Thread Dan Loewenherz via swift-users
You'll need to specify a protocol where you're currently specifying "Bool". A generic constraint can't take a concrete type as an argument. So you could do this: protocol BooleanType {} extension Bool: BooleanType {} extension foo where T: BooleanType { ... } If you don't like this, you can d

Re: [swift-users] Attempting to call default protocol implementation crashes Playground

2016-11-15 Thread Dan Loewenherz via swift-users
What are you trying to accomplish here, more concretely? My first thought is that you shouldn't implement the same function in both a protocol extension and a conforming class. Why not just give them different names and call the function from within the extension instead of from the class? E.g. p

Re: [swift-users] NSJSONSerialization barfs writing a Dictionary parsed from JSON: "Invalid type in JSON write (_SwiftValue)"

2016-11-18 Thread Dan Loewenherz via swift-users
I've encountered a similar bug and filed a radar a few months ago. My report was marked as a duplicate but besides that I haven't received any follow-up. It does appear to have been fixed in Xcode 8.1 / Swift 3.0.1. http://www.openradar.me/28365419 Essentially, NSJSONSerialization would (does?) cr