2

Is there any way to describe in Swift an IntegerType that has a max property? (something similar to implicit interfaces in go)

There is no protocol to describe a max attribute, and even if I create one, IntegerType does not explicitly implement it.

So basically I'm looking for something like:

class Test<T: IntegerType where ?> { // <- ? = something like 'has a "max: Self"' property
}

let t = Test<UInt8>()

or maybe something like:

implicit protocol IntegerTypeWithMax: IntegerType {
    static var max: Self { get }
}

class Test<T: IntegerTypeWithMax> {
}

let t = Test<UInt8>()
rid
  • 61,078
  • 31
  • 152
  • 193
  • Just remove the "implicit" and then it compiles ... – Martin R Jun 21 '15 at 10:46
  • @MartinR, it compiles, but it doesn't do what I would like it to do, since `IntegerType` does not implement `IntegerTypeWithMax`, so I can't instantiate a class with any `IntegerType` (such as `UInt8`) as parameter. – rid Jun 21 '15 at 10:48
  • The compiler does not know if all types conforming to `IntegerType` have a `max` property. You have to tell him with e.g. `extension UInt8 : IntegerTypeWithMax { }`. (There may be a better way with the new protocol extensions in Swift 2.) – Martin R Jun 21 '15 at 10:51
  • @MartinR, exactly, that's what I was wondering - is there any way to describe an `IntegerType` with a `max` property, as opposed to just any `IntegerType`, like you can do in go (which has the notion of implicit interface), or TypeScript for example? – rid Jun 21 '15 at 10:53
  • I have no experience with go, so I cannot compare, but I think I understand now what you mean. In Swift you *can* defined a IntegerTypeWithMax protocol, but the compiler does not automatically infer that `UInt8` conforms to that protocol just because it has a `max` method. You have to tell him. – Martin R Jun 21 '15 at 10:56
  • @MartinR, so in Swift what you would do would be to declare a `IntegerTypeWithMax` protocol, then extend all the `IntegerType`s you're interested in to implement the protocol? – rid Jun 21 '15 at 10:58
  • Yes. See http://stackoverflow.com/questions/25575513/what-protocol-should-be-adopted-by-a-type-for-a-generic-function-to-take-any-num for an example. – Martin R Jun 21 '15 at 10:59
  • @MartinR, great, sounds good. Can you please add this as an answer? – rid Jun 21 '15 at 11:00

1 Answers1

3

The Swift compiler does not automatically infer protocol conformance even if a type implements all the required properties/methods. So if you define

protocol IntegerTypeWithMax: IntegerType {
    static var max: Self { get }
}

you still have to make the integer types that you are interested in conform to that protocol:

extension UInt8 : IntegerTypeWithMax { }
extension UInt16 : IntegerTypeWithMax { }
// ...

The extension block is empty because UInt8, UInt16 already have a static max method.

Then

class Test<T: IntegerTypeWithMax> {
}

let t = Test<UInt8>()

compiles and works as expected.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382