|
Revision 2127, 0.8 KB
(checked in by Chuck.Esterbrook, 3 years ago)
|
|
Take out is shared on def main in the how-to's.
|
-
Property svn:eol-style set to
native
|
| Line | |
|---|
| 1 | """ |
|---|
| 2 | How to declare and use variable number of arguments: |
|---|
| 3 | |
|---|
| 4 | * Put the `vari` keyword before the type of the argument. |
|---|
| 5 | * Use a `for` loop to enumerate through them. |
|---|
| 6 | |
|---|
| 7 | You don't need to specify the type of the variable in the `for` loop. Cobra |
|---|
| 8 | infers that from what is being enumerated. |
|---|
| 9 | |
|---|
| 10 | Also the var args are compatible with IEnumerable<of T> and IEnumerable. |
|---|
| 11 | So you can pass the argument wherever those types are expected. |
|---|
| 12 | """ |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | class Test |
|---|
| 16 | |
|---|
| 17 | def allTrue(stuff as vari dynamic?) as bool |
|---|
| 18 | """ Returns true only if all the stuff passed in is true. """ |
|---|
| 19 | for x in stuff, if not x, return false |
|---|
| 20 | return true |
|---|
| 21 | |
|---|
| 22 | def sumInts(stuff as vari int) as int |
|---|
| 23 | """ Return the sum of all the ints. """ |
|---|
| 24 | sum = 0 |
|---|
| 25 | for x in stuff, sum += x |
|---|
| 26 | return sum |
|---|
| 27 | |
|---|
| 28 | def main |
|---|
| 29 | t = Test() |
|---|
| 30 | assert t.allTrue(1, 3.0, true) |
|---|
| 31 | assert not t.allTrue(1, 0, true) |
|---|
| 32 | assert t.sumInts(1, 2, 3)==6 |
|---|