Just An Application

July 3, 2014

So Swift Then: Tuples – One More Thing

A tuple value is an example of a parenthesized expression the syntax for which is as follows

    parenthesized-expression : '(' expression-element-list? ')'
    
    expression-element-list : expression-element
                            | expression-element ',' expression-element-list
                            
    expression-element      : expression
                            | identifier ':' expression

In the case of a tuple value, if the expression-element is always an expression you get an ordinary tuple,

    ("cod", "dab", "eel")

if the expression-element is always an identifier a colon and an expression you get a tuple with named values,

    (first:"cod", second:"dab", third:"eel")

but the syntax at least does not preclude having a mixture of both.

This would seem to imply that you should be able to do something like this

    let mixedTuple = (first:"cod", "dab", third:"eel")

and interestingly enough a little experimentation reveals that you can do exactly that.

At the moment I can’t think why you might want to do this but if you can think of a reason then currently at least you will be able to.


Copyright (c) 2014 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Blog at WordPress.com.