[swift-evolution] Specify type of a delegate which conforms to a protocol
davesweeris at mac.com
davesweeris at mac.com
Tue Feb 9 14:00:31 CST 2016
That might solve Inder’s problem, but strictly speaking it doesn’t actually restrict the type to an enum. This struct meets all of RawRepresentable’s requirements:
struct Foo : RawRepresentable {
typealias RawValue = String
init?(rawValue: Foo.RawValue) {
return nil
}
var rawValue: RawValue = "bar"
}
(Although in practice I can’t think of why that would matter, since you can’t do anything with a RawRepresentable other than get it’s rawValue or call init?(rawValue: String), and neither of those rely on enum features… I’ll stop being pedantic now.)
Anyway, the bigger point is that there’s no way to restrict a generic type to be an enum. I think it’s because there wouldn’t be a way to switch on it (or do other enum-ish things) without knowing all its cases, which requires knowing exactly which type it is, which means it’s no longer a generic type. That’s just a guess, though.
Maybe someone should propose that we allow something like this:
func foobar <T, U where T: (case .foo, case .bar)> (value: T) -> U {
switch value {
case .foo: ...
case .bar: ...
}
}
- Dave Sweeris
> On Feb 9, 2016, at 10:47, Alex Hoppen via swift-evolution <swift-evolution at swift.org> wrote:
>
> As for a new syntax, I was also thinking about this issue a few days ago and it occurred to me that there are even more types that cannot be properly represented in Swift. For example there is currently no way (that I know of) to create a variable that can contain any enum that is backed by a String. However, these kinds of constraints can easily be specified in generic constraints (the ones in angle brackets). Maybe we could add the same syntax for variables/constants as well so that the code would look something like the following for your issue:
>
> var<T, where T: UIViewController, T: MyProtocol> myVar: T
>
> or for enums backed by a String:
>
> var<T: RawRepresentable where T.RawValue == String> myVar: T
>
> Tell me what you think about it.
>
> - Alex
>
>
>> On 09 Feb 2016, at 15:58, Inder Kumar Rathore . via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Hi All,
>> I used to do this in Obj-C but I'm unable to do this in swift
>>
>> UIViewController<MyProtocol> *delegate;
>>
>> I posted this question on dev forums but didn't get the solution and finally I'm reporting here so that It can be added as a features in the coming releases.
>>
>> Thanks
>>
>> Rathore
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160209/b261dfdb/attachment.html>
More information about the swift-evolution
mailing list