| 1 | = Multi Variable or Multi Target Assignments = |
| 2 | |
| 3 | Cobra supports multi variable or multi target assignment |
| 4 | {{{ |
| 5 | a,b,c = 10,20,30 # comma separated list of expressions |
| 6 | |
| 7 | # or |
| 8 | a,b,c = [10,20,30] # list literal |
| 9 | |
| 10 | # or |
| 11 | alist = [10,20,20] |
| 12 | a,b,c = alist # an arbitrary list variable |
| 13 | |
| 14 | assert a == 10 |
| 15 | assert b == 20 |
| 16 | assert c == 30 |
| 17 | }}} |
| 18 | These are expected to give the same results as if the assignment |
| 19 | target and source lists were unrolled and each assignment done individually |
| 20 | {{{ |
| 21 | a = 10 |
| 22 | b = 20 |
| 23 | c = 30 |
| 24 | |
| 25 | a = alist[0] |
| 26 | b = alist[1] |
| 27 | c = alist[2] |
| 28 | }}} |
| 29 | |
| 30 | The targets must be a comma separated list, the items of which are |
| 31 | things that can be assigned a value - any of local variables, |
| 32 | class or instance variables, properties or indexers. |
| 33 | |
| 34 | The assignment sources can be a single expression that resolves to a list, array or |
| 35 | String (specifically any expression that supports an integer indexer)[[BR]] |
| 36 | '''or''' |
| 37 | a comma separated stream of expressions. |
| 38 | |
| 39 | {{{ |
| 40 | # unusual but acceptable - dict keyed by int in range |
| 41 | dict = {1:'aye', 2:'bee'} |
| 42 | a,b = dict |
| 43 | assert a == 'aye' |
| 44 | assert b == 'bee' |
| 45 | |
| 46 | # assignment to an expression list |
| 47 | a,s,c = 99+1, 'xxx'+'y', 2*2 |
| 48 | assert a == 100 |
| 49 | assert x == 'xxxy' |
| 50 | assert c == 4 |
| 51 | |
| 52 | }}} |
| 53 | |
| 54 | The size of the target and source lists are expected to match as if the |
| 55 | assignments were done individually. |
| 56 | |
| 57 | The target source values are atomic in value for the duration of the assignments. |
| 58 | |
| 59 | == multi Targets in a 'for loop' == |
| 60 | |
| 61 | Multiple targets can also be provided in a for loop covering an enumerable |
| 62 | source. |
| 63 | |
| 64 | If the enumerable source is a Dictionary there may be only 2 targets and they |
| 65 | get filled with the next key and keyValue from the dictionary for the loop |
| 66 | block |
| 67 | |
| 68 | {{{ |
| 69 | dict = {'x':'aye', 'y':'bee'} |
| 70 | for k,v in dict |
| 71 | print 'key=,k, 'value=', v |
| 72 | assert k in '[x', 'y'] |
| 73 | assert v in ['aye', 'bee'] |
| 74 | }}} |
| 75 | |
| 76 | Otherwise the enumerable source is expected to generate a list of items of |
| 77 | the same size as the list of targets for each step of the enumeration |
| 78 | {{{ |
| 79 | for i,j in [1,2,3,4] # error |
| 80 | pass |
| 81 | |
| 82 | for i,j in [ [1,2], [3,4]] |
| 83 | assert i in [1,3] |
| 84 | assert j in [2,4] |
| 85 | |
| 86 | }}} |
| 87 | |
| 88 | If you want to get multiple values from an enumeration generating non list |
| 89 | (single) values the current idiom is to use a |
| 90 | [wiki:EnumForExpr for expression] |
| 91 | {{{ |
| 92 | i,j = for x in [1,2,3,4] get x |
| 93 | |
| 94 | alist = [1,2,3,4] |
| 95 | i,j = for x in alist get x |
| 96 | }}} |