specialize a generic type in a method


(Vladimir) #1

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)


(Charlie Monroe) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Vladimir) #3

Thank you for reply. Yes, seems this could be solved by extension. No other solutions?

But how should I be if I need such a protocol?

protocol P1 {
     associatedtype T
     func foo(t: T)
     func bar(t: T) // where T:Equatable ??
}

I.e. I need to specify T & Equatable for bar in requirements

···

On 20.06.2016 20:03, Charlie Monroe wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Charlie Monroe) #4

Something like this should work:

protocol P1 {
  associatedtype T
  func foo(t: T)
  func bar<U where U == T, U: Equatable>(t: U)
}

···

On Jun 20, 2016, at 8:07 PM, Vladimir.S <svabox@gmail.com> wrote:

Thank you for reply. Yes, seems this could be solved by extension. No other solutions?

But how should I be if I need such a protocol?

protocol P1 {
   associatedtype T
   func foo(t: T)
   func bar(t: T) // where T:Equatable ??
}

I.e. I need to specify T & Equatable for bar in requirements

On 20.06.2016 20:03, Charlie Monroe wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Vladimir) #5

Something like this should work:

protocol P1 {
  associatedtype T
  func foo(t: T)
  func bar<U where U == T, U: Equatable>(t: U)
}

Thank you. Strange, but also can't check this code - compiler crashes on it.

IMO it will be good if we can write just

func bar(t: T & Equatable)

···

On 20.06.2016 21:38, Charlie Monroe wrote:

On Jun 20, 2016, at 8:07 PM, Vladimir.S <svabox@gmail.com> wrote:

Thank you for reply. Yes, seems this could be solved by extension. No other solutions?

But how should I be if I need such a protocol?

protocol P1 {
   associatedtype T
   func foo(t: T)
   func bar(t: T) // where T:Equatable ??
}

I.e. I need to specify T & Equatable for bar in requirements

On 20.06.2016 20:03, Charlie Monroe wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Vladimir) #6

This may change a lot if once existentials proposal gets approved and implemented...

Something like this should work:

protocol P1 {
  associatedtype T
  func foo(t: T)
  func bar<U where U == T, U: Equatable>(t: U)
}

Seems like your suggestion will not work. Doug Gregor : "This should be ill-formed. "bar" isn't actually a generic function. If we want this behavior, we should explicitly support it with "where" clauses on non-generic declarations." (https://bugs.swift.org/browse/SR-1849)

Checking if there is another solution for this.

···

On 21.06.2016 18:59, Charlie Monroe wrote:

On Jun 21, 2016, at 5:20 PM, Vladimir.S <svabox@gmail.com> wrote:
On 20.06.2016 21:38, Charlie Monroe wrote:

Thank you. Strange, but also can't check this code - compiler crashes on it.

IMO it will be good if we can write just

func bar(t: T & Equatable)

On Jun 20, 2016, at 8:07 PM, Vladimir.S <svabox@gmail.com> wrote:

Thank you for reply. Yes, seems this could be solved by extension. No other solutions?

But how should I be if I need such a protocol?

protocol P1 {
  associatedtype T
  func foo(t: T)
  func bar(t: T) // where T:Equatable ??
}

I.e. I need to specify T & Equatable for bar in requirements

On 20.06.2016 20:03, Charlie Monroe wrote:

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution