File: vararg.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (76 lines) | stat: -rw-r--r-- 3,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// RUN: %target-typecheck-verify-swift

var t1a: (Int...) = (1)
// expected-error@-1 {{variadic parameter cannot appear outside of a function parameter list}}
var t2d: (Double = 0.0) = 1 // expected-error {{default argument not permitted in a tuple type}} {{18-23=}}

func f1(_ a: Int...) { for _ in a {} }
f1()
f1(1)
f1(1,2)
func f2(_ a: Int, _ b: Int...) { for _ in b {} }
f2(1)
f2(1,2)
f2(1,2,3)

func f3(_ a: (String) -> Void) { }
f3({ print($0) })


func f4(_ a: Int..., b: Int) { }

// rdar://16008564
func inoutVariadic(_ i: inout Int...) {  // expected-error {{'inout' must not be used on variadic parameters}}
}

// rdar://19722429
func invalidVariadic(_ e: NonExistentType) { // expected-error {{cannot find type 'NonExistentType' in scope}}
  { (e: ExtraCrispy...) in }() // expected-error {{cannot find type 'ExtraCrispy' in scope}}
}

func twoVariadics(_ a: Int..., b: Int...) { }
func unlabeledFollowingVariadic(_ a: Int..., _ b: Int) { } // expected-error {{a parameter following a variadic parameter requires a label}}
func unlabeledVariadicFollowingVariadic(_ a: Int..., _ b: Int...) { } // expected-error {{a parameter following a variadic parameter requires a label}}
func unlabeledFollowingTwoVariadics(_ a: Int..., b: Int..., _ c: Int) { } // expected-error {{a parameter following a variadic parameter requires a label}}
func splitVariadics(_ a: Int..., b: Int, _ c: String...) { }
func splitByDefaultArgVariadics(_ a: Int..., b: Int = 0, _ c: String...) { }

struct HasSubscripts {
  subscript(a: Int...) -> Void { () }
  subscript(a: Int..., b b: Int...) -> Void { () }
  subscript(a: Int..., b: Int...) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}}
  subscript(a: Int..., b: Int) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}}
  subscript(a: Int..., b b: Int..., c c: Int) -> Void { () }
  subscript(a: Int..., b b: Int..., c: Int) -> Void { () } // expected-error {{a parameter following a variadic parameter requires a label}}
  subscript(a: Int..., c c: Int = 0, b: Int...) -> Void { () }
  subscript(a: Int..., b: String = "hello, world!") -> Bool { false } // expected-error {{a parameter following a variadic parameter requires a label}}
}

struct HasInitializers {
  init(a: Int...) {}
  init(a: Int..., b: Int...) {}
  init(a: Int..., _ b: Int...) {} // expected-error {{a parameter following a variadic parameter requires a label}}
  init(a: Int..., c: Int = 0, _ b: Int...) {}
}

let closure = {(x: Int..., y: Int...) in } // expected-error {{no parameters may follow a variadic parameter in a closure}}
let closure2 = {(x: Int..., y: Int) in } // expected-error {{no parameters may follow a variadic parameter in a closure}}
let closure3 = {(x: Int..., y: Int, z: Int...) in } // expected-error {{no parameters may follow a variadic parameter in a closure}}
let closure4 = {(x: Int...) in }
let closure5 = {(x: Int, y: Int...) in }
let closure6 = {(x: Int..., y z: Int) in } // expected-error {{closure cannot have keyword arguments}}
// expected-error@-1 {{no parameters may follow a variadic parameter in a closure}}

// rdar://22056861
func f5(_ list: Any..., end: String = "") {}
f5(String())

// rdar://18083599
enum E1 {
  case On, Off
}

func doEV(_ state: E1...) {}
doEV(.On)

func hasDefaultArg(a: Int... = [5]) {} // expected-error {{variadic parameter cannot have a default value}}