News for hackers. Stuff that matters.
Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @05:57AM (#10743474)
I'm not a Swift person, so I might be wrong, but this seems like a case where composition over inheritance is preferred. Protocols in Swift (and traits in Rust) are not supposed to be used to emulate classical inheritance; though they can be. Enums/ADTs/sum types/whatever you call them are what you're supposed to use in such cases.
Re:Limitations of protocols in Swift by alexjarvis on Wednesday December 16, 2015 @06:04AM (#10743503)
Yeah you're right. If you want to stick with value types and store those values in an Array where the array can only store one type then you have to use an Enum instead of classes. I think there are a few people getting confused by not identifying enums in Swift as a more powerful construct than in C/ObjC.
Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @06:12AM (#10743520)
We basically have that same issue in Rust :)
Re:Limitations of protocols in Swift by alexjarvis on Wednesday December 16, 2015 @06:18AM (#10743545)
And with associated values in enum types they behave in a similar way to case classes in Scala that extend a sealed trait.

For example I've been working on a Json library that stores everything in an enum (ADT):

  public enum JsValue {
    case JsString(String)
    case JsNumber(Double)
    case JsObject([String: JsValue])
    case JsArray([JsValue])
    case JsBoolean(Bool)
    case JsNull
  }
Re:Limitations of protocols in Swift by kpmah on Wednesday December 16, 2015 @06:24AM (#10743561)
Don't trait objects (https://doc.rust-lang.org/stable/reference.html#trait-object...) solve this problem?
Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @06:45AM (#10743634)
Well, yes. But that wasn't what I was talking about.

Trait objects in Rust let you emulate inheritance. The issue I was talking about is that folks from inheritance-heavy languages use this as the de facto design pattern, and then stuff doesn't work so well, because you're not supposed to use inheritance so much (the languages aren't designed with this in mind). The correct option is to use enums and composition over inheritance wherever possible.

Re:Limitations of protocols in Swift by seanmcdirmid on Wednesday December 16, 2015 @07:10AM (#10743743)
In functional languages, functionality should be extrinsic (case match values from the outside), while in OO languages like functionality is intrinsic (polymorphic method members called on the value). However, for things like hashing, intrinsic works way better (hence traits, protocols, type classes, and so on).

Saying use composition over inheritance "wherever possible" can lead to some cargo culting on its own. It is always possible to use composition over inheritance, but it isn't always wise.

Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @07:22AM (#10743780)
> but it isn't always wise.

Pretty much what I meant, when I said "wherever possible" I meant "wherever possible with decent code", sorry about the confusion.

Re:Limitations of protocols in Swift by fauigerzigerk on Wednesday December 16, 2015 @06:34AM (#10743600)
I disagree. This is the classic case for protocols. Look what he says:

"I’m using a protocol for the Account type. There are classes that conform to the Account protocol: TwitterAccount, AppNetAccount, LinkedInAccount, etc."

The important bit is the "etc." at the end. The types of accounts he wants to support is open ended. He also mentions a plug-in API. You can't have third parties come in and extend some algebraic data type.

If this is not a case for protocols then there is no such case at all and protocols should be removed from the language.

Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @06:55AM (#10743673)
> protocols should be removed from the language.

That's a bit harsh. You forget that protocols are useful for generics too.

But yes, if they want it to be extensible, protocols would be the way to go.

Looking deeper into the issue, it seems like this isn't really an issue with protocols, just an issue with the design of Hashable or Equatable (either allow Equatable to work with protocol objects, or move the Equatable dependency of Hashable to a different kind of Equatable).

This sounds like a nitpick, but it's the difference between language and API design, and the latter is often easier to evolve. It also means that this can be fixed by a ProtocolHashSet<T> library which does hashing differently without touching the compiler/stdlib at all.

Re:Limitations of protocols in Swift by captaincrowbar on Wednesday December 16, 2015 @08:19AM (#10744039)
To me this looks like exactly the sort of situation where classical OO inheritance is the right answer. All of those things are kinds of Account and should have a common base class.
Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @10:12AM (#10744689)
Classical OO inheritance is the right answer in a language that supports it, though. There may be equally ergonomic ways of doing it in languages with ADTs.

(Which may or may not be the case here, depending on extensibility)

Re:Limitations of protocols in Swift by panic on Wednesday December 16, 2015 @06:06AM (#10743509)
He mentions a plug-in API in the second linked post. Enums would be a good solution, but they can't be extended with more cases by a plug-in.
Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @06:22AM (#10743558)
Yeah, that's when you would use trait objects in Rust (and virtual dispatch protocols in Swift?), but you still probably won't use them exactly the same way.
Re:Limitations of protocols in Swift by alexjarvis on Wednesday December 16, 2015 @06:39AM (#10743617)
extensions let you do this with enums (as well as protocols):

  enum Car { 
    case Red 
    case Blue 
  }    
  extension Car { 
    func hello() { 
      print("hello") 
    } 
  } 
  let x = Car.Blue
  x.hello()
https://developer.apple.com/library/ios/documentation/Swift/...

edit: although you can't add a new case to the enum.

Re:Limitations of protocols in Swift by Manishearth on Wednesday December 16, 2015 @06:43AM (#10743626)
> although you can't add a new case to the enum.

That's the point. We all know you can add impls or extensions.