[swift-evolution] Generic parameters in "as?" checks

David Turnbull dturnbull at gmail.com
Thu Jan 21 03:52:35 CST 2016


This sounds like it could lead down a path that would clean up some ugly
code I wrote recently.

public protocol VectorType : GLmathType {
    typealias FloatVector
    typealias DoubleVector
    typealias Int32Vector
    typealias UInt32Vector
    typealias BooleanVector

This is the protocol for vector2/3/4 types. Some functions need to return a
bool result. Others are an unsafe cast to another type. I'm also using the
BooleanVector as a key where I need vectors of the same length which have
different elements.

public func notEqual<genType:ScalarVectorType where
    genType.BooleanVector.Element == Bool,
    genType.BooleanVector == genType.BooleanVector.BooleanVector
    >(x:genType, _ y:genType) -> genType.BooleanVector {
        return genType.BooleanVector(x, y, !=)
}

Many times I went looking for any way to get back to the raw generic type.
I don't know if this makes sense out of context, so here's the full set of
prototypes:
https://github.com/AE9RB/SwiftGL/blob/master/Sources/SwiftGLmath/Types.swift

I hope this is helpful to those of you thinking about changes. It's really
far down on my wish list though. The "expression too complex" errors and
compile times are blocking me right now.

-david

On Wed, Jan 20, 2016 at 10:21 PM, Charles Srstka via swift-evolution <
swift-evolution at swift.org> wrote:

> On Jan 20, 2016, at 11:56 PM, Slava Pestov <spestov at apple.com> wrote:
>
>
> Okay, in the example above, that leaves five places for the generic
> signature to go: after the “if”, after the “let”, after the variable name,
> after the equals sign, and after the “as?”. Or perhaps we could introduce
> some sort of new keyword in there to which to attach the generic signature.
>
>
> Various approaches have been discussed before. The one that’s closest to
> what you describe above is to have some kind of ‘open’ keyword,
>
> func foo(p: SomeProto) {
>   let t = open p as T {
>     T.SomeAssocType.someStaticMethod(…)
>   }
> }
>
>
> Thanks, this looks good. Based on this, I found the earlier discussion
> where this came up, but it seems that the example above assumes you already
> know p is SomeProto. I assume there’d be a way to take p as something else,
> like an Any, and dynamically cast it to SomeProto using some combination of
> as? and open, right?
>
> Is this a feasible thing to ask for?
>
>
> I think so, but we have to careful with the design. This ties in with some
> of the discussions on generalizing existential types, also.
>
>
> That’s good to hear. Is there any information on what the Swift team is
> planning to do with existentials?
>
>
> Oh, I was just referring to the discussion on this list. No concrete
> proposals have been accepted so far, AFAIK.
>
>
> Well, if someone proposes one, it’ll get a +1 from me.
>
> Thanks,
> Charles
>
>
> _______________________________________________
> 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/20160121/ab42b43f/attachment.html>


More information about the swift-evolution mailing list