[swift-evolution] specialize a generic type in a method

Charlie Monroe charlie at charliemonroe.net
Mon Jun 20 12:03:18 CDT 2016


This is IMHO better solved via an extension - it even makes more sense to put together methods whose use is limited only to certain generics constraint:

extension Foo where I: Equatable {

	func bar(i: I) {
		///
	}

}


> On Jun 20, 2016, at 6:57 PM, Vladimir.S via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Should we be able to specialize a generic type in a method, if that generic type declared in type's definition like here? :
> 
> struct Foo<I> {
>    func bar(i: I) where I: Equatable { } // should this work?
> }
> 
> As I understand, for concrete instance of foo: Foo<X>, if X is not Equatable, then compiler should prevent us from calling foo.bar(x)
> 
> Or, probably, the better way to express this could be :
> 
>    func bar(i: I & Equatable) { }
> or
>    func bar(i: Any<I, Equatable>) { }
> 
> Can't check if it possible now because my compiler crashes because of 'where I:Equatable' text (submitted to bugs.swift.org)
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list