[swift-evolution] Allow using protocols with concrete associated types as non-generic variable types

Austin Feight austin at chexology.com
Mon Apr 24 14:43:52 CDT 2017


The following code won't compile for reasons we're familiar with:

protocol Value {
  associatedType ValueType
  var value: ValueType { get set }
}

struct ValueHolder {
  var value: Value  // Protocol 'Value' can only be used as a generic
constraint because it has Self or associated type requirements
}

But then when I make that associated type concrete in a more sub-protocol,
the compiler still complains, even when it should no longer have associated
type requirements, because ValueType can only be Int.

protocol IntValue: Value {
  associatedType ValueType = Int
  var value: Int { get set }
}

struct ValueHolder {
  var value: IntValue  // Protocol 'IntValue' can only be used as a
generic constraint because it has Self or associated type requirements
}

or at least - that's what I'm trying to achieve with `associatedType
ValueType = Int`. Similarly I tried `typealias ValueType = Int`, with the
same result.

Would this be considered a compiler bug? Or is IntValue's ValueType
shadowing Value's ValueType, and they're both still independent? I don't
believe the latter case is correct because even when the original
declaration ValueType is concrete, the compiler still complains:

protocol IntValue {
  associatedType ValueType = Int
  var value: Int { get set }
}

struct ValueHolder {
  var value: IntValue  // Protocol 'IntValue' can only be used as a
generic constraint because it has Self or associated type requirements
}

Trying to figure out where this proposal would fall, or if it's been
discussed already - I wasn't able to find anything similar.


Cheers,
*Austin Feight *| Chief Shipping Officer


150 E. 52nd Street, 22nd Floor, New York, NY 10022
www.chexology.com <http://www.coatchex.com/> | www.austindfeight.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170424/13184737/attachment.html>


More information about the swift-evolution mailing list