Fascinating, thank you. I tried to do the two-type func+ generic, but I was 
getting complaints from the compiler. I wonder what I did wrong.

So, Swift will preferentially select the single-type func+? That makes sense, 
of course, but is it defined in the language spec to do so? Is that documented 
anywhere?

Thanks!

> On Jun 24, 2015, at 13:21 , Jonathan Hull <jh...@gbis.com> wrote:
> 
> Here is some working code for a playground:
> 
> protocol VectorElementType: IntegerLiteralConvertible
> {
>     func +(lhs: Self, rhs: Self) -> Self
>     func toDouble()->Double
> }
> 
> struct Vector3<T where T: VectorElementType>:CustomStringConvertible
> {
>     init()
>     {
>         x = 0;
>         y = 0;
>         z = 0;
>     }
>     
>     init(_ inX: T, _ inY: T, _ inZ: T)
>     {
>         x = inX
>         y = inY
>         z = inZ
>     }
>     
>     var description:String {
>         return "<\(x),\(y),\(z)>"
>     }
>     
>     var x: T
>     var y: T
>     var z: T
> }
> 
> func +<T>(inLeft: Vector3<T>, inRight: Vector3<T>) -> Vector3<T>
> {
>     return Vector3<T>(inLeft.x + inRight.x, inLeft.y + inRight.y, inLeft.z + 
> inRight.z)
> }
> 
> 
> //I added this call which it falls back to when the types don’t match
> func +<T,S where T:VectorElementType,S:VectorElementType>(inLeft: Vector3<T>, 
> inRight: Vector3<S>) -> Vector3<Double>
> {
>     return Vector3<Double>(inLeft.x.toDouble() + inRight.x.toDouble(), 
> inLeft.y.toDouble() + inRight.y.toDouble(), inLeft.z.toDouble() + 
> inRight.z.toDouble())
> }
> 
> 
> extension Float: VectorElementType {
>     func toDouble() -> Double {
>         return Double(self)
>     }
> }
> extension Double: VectorElementType {
>     func toDouble() -> Double {
>         return self
>     }
> }
> 
> typealias Vector3f = Vector3<Float>
> typealias Vector3d = Vector3<Double>
> 
> var a = Vector3d(1, 2, 3)
> var b = Vector3f(4, 5, 6)
> 
> var c = a + b               //<5.0,7.0,9.0>
> print(c.x.dynamicType)//Prints Swift.Double
> 
> var d = b + b         //<8.0,10.0,12.0>
> print(d.x.dynamicType)//Prints Swift.Float
> 
> 
> 
> 
>> On Jun 24, 2015, at 12:19 PM, Rick Mann <rm...@latencyzero.com> wrote:
>> 
>> I've been experimenting with Swift 2, and have started writing a generic 
>> Vector3<T> class. It looks something like this:
>> 
>> -----
>> protocol
>> VectorElementType: IntegerLiteralConvertible
>> {
>>      func +(a: Self, b: Self) -> Self
>> }
>> 
>> struct
>> Vector3<T where T: VectorElementType>
>> {
>>      init()
>>      {
>>              x = 0;
>>              y = 0;
>>              z = 0;
>>      }
>>      
>>      init(_ inX: T, _ inY: T, _ inZ: T)
>>      {
>>              x = inX
>>              y = inY
>>              z = inZ
>>      }
>>      
>>      var x: T
>>      var y: T
>>      var z: T
>> }
>> 
>> func
>> +<T>(inLeft: Vector3<T>, inRight: Vector3<T>)
>>      -> Vector3<T>
>> {
>>      return Vector3<T>(inLeft.x + inRight.x, inLeft.y + inRight.y, inLeft.z 
>> + inRight.z)
>> }
>> 
>> 
>> extension Float: VectorElementType {}
>> extension Double: VectorElementType {}
>> 
>> typealias Vector3f = Vector3<Float>
>> typealias Vector3d = Vector3<Double>
>> -----
>> 
>> The thing I'd like to do is let the + operator support two different types 
>> of Vector3, such that if the individual VectorElementTypes are addable 
>> together (either because a + operator exists for both types, or because one 
>> type can be promoted to a type that can add), then it all "just works".
>> 
>> In C++, this works because template instantiation happens when the types are 
>> introduced, but in Swift, I have to promise that the types will work out 
>> that way. But I've not figured out how.
>> 
>> E.g., I can't do this:
>> 
>> var a = Vector3d(1, 2, 3)
>> var b = Vector3f(4, 5, 6)
>> 
>> var c = a + b
>> 
>> How might I accomplish this? Thanks!
>> 
>> 
>> -- 
>> Rick Mann
>> rm...@latencyzero.com
>> 
>> 
>> 
>> _______________________________________________
>> 
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>> 
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>> 
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/jhull%40gbis.com
>> 
>> This email sent to jh...@gbis.com
> 


-- 
Rick Mann
rm...@latencyzero.com



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to