[swift-evolution] Tuple conversion and type composition

Joe Groff jgroff at apple.com
Mon Feb 8 10:21:31 CST 2016


If we had a sufficiently generalized "splat" operation to unpack tuples into argument or tuple lists, you could conceivably use it to do this:

typealias AB = (A,B)
typealias CD = (C,D)
typealias ABCD = (AB..., CD...)

func join<T, U>(_ a: (T...), _ b: (U...)) -> (T..., U...) {
  return (a..., b...)
}

func pop<T, U>(_ tuple: (T..., U)) -> ((T...), U) {
  let (a..., b) = tuple
  return (a, b)
}

func push<T, U>(_ tuple: (T...), _ back: U) -> (T..., U) {
  return (tuple..., back)
}

-Joe

> On Jan 30, 2016, at 9:14 PM, Andrew Bennett via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi Swift Evolution populace,
> 
> I'd like a a way to concatenate tuple types together:
> 
> typealias ABCD = (A,B)+(C,D) // Same as (A,B,C,D)
> Also a way to allow tuples to be converted to other tuples that are the same when flattened:
> 
> (a,(b,c),d) as ((a,b),(c,d))
> This proposal aims to make the minimal changes possible, with the most familiar syntax, and give tuples this power.
> 
> Examples
> 
> These are things that will be possible after these changes.
> Joining two tuples together
> 
> let a = (1,2), b = (3,4)
> let c = join(a, b) // (Int,Int,Int,Int)
> 
> Stack like operations on tuples
> 
> let (abcde)  = (1,2,3,4,5)
> let (abcd,e) = pop(abcde)
> let (abc,d)  = pop(abcd)
> let (abce)   = push(abc, e)
> assert(abce == (1,2,3,5))
> Chained ZipSequence
> 
> let a = [1,2,3]
> let b = ["a", "b", "c"]
> let c = [1.2, 2.3, 3.4]
> let d = [nil, nil, 123]
> let abc = a.zip(b).zip(c).zip(d) // AnySequence<(Int,String,Float,Int?)>
> Turn any function with a callback into one with a promise
> 
> func promise<A,B,C>(f: (A + (B->Void)) -> C) -> A -> (C, Promise<B>)
> This proposal, and more examples, can be seen in more detail here:
> 
>     https://github.com/therealbnut/swift-evolution/blob/therealbnut-tuple-manipulation/proposals/0000-tuple-operators.md <https://github.com/therealbnut/swift-evolution/blob/therealbnut-tuple-manipulation/proposals/0000-tuple-operators.md>
> 
> I'll keep this file up to date over the course of the discussion, PRs are welcome.
> 
> Related proposals:
> 
> This proposal's use cases relate to at least a few other currently active proposals:
> Compile-time parameters
> http://thread.gmane.org/gmane.comp.lang.swift.evolution/5240 <http://thread.gmane.org/gmane.comp.lang.swift.evolution/5240>
> Contiguous Variables (A.K.A. Fixed Sized Array Type)
> http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809 <http://thread.gmane.org/gmane.comp.lang.swift.evolution/4809>
> Remove implicit tuple splat behavior from function applications
> http://thread.gmane.org/gmane.comp.lang.swift.evolution/4681 <http://thread.gmane.org/gmane.comp.lang.swift.evolution/4681>
> Thanks!
> _______________________________________________
> 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/20160208/44adb643/attachment.html>


More information about the swift-evolution mailing list