[swift-evolution] I think we need more to our type calculus

Joe Groff jgroff at apple.com
Thu Feb 9 12:44:42 CST 2017

> On Feb 8, 2017, at 12:09 PM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
> I’ve been trying to get the maximum of a list of counts. I started by using “myCollection.map { $0.myCountProperty }.reduce(0, max)”. Then I thought I should really use the value closest to negative infinity as the base. The problem is how to get that value. The current hard-coded “0” works because the count type ultimately aliases “Int”, but shouldn’t have to count on that. I put in “MyCountType.min”, where “MyCountType” is hard coded from the docs of “myCountProperty”, but I shouldn’t have to do that either.
> There is a “type(of:)” global function, which I did use. But not only did I have to make up an expression to go in there, its return value, some sort of meta-type stuff I don’t understand, can’t be used on the right side of a “typealias” construct. I ultimately used “let lowestCount = type(of: anElement.myCountProperty).min” to get what I needed.

type(of:) does evaluate its argument, since for things like classes you need to know the value you're asking of a type for. However, you can avoid hardcoding a type at all in this case by using contextual lookup:

myCollection.map { $0.myCountProperty }.reduce(.min, max)

`.foo` will look up static members inside the contextual type of the expression.


More information about the swift-evolution mailing list