Reputation: 2154
I want to achieve something like
var a, b, c: MyType = MyType()
but this line doesn't compile because compiler treats the type annotation MyType
is only for variable c
thus a
and b
are missing either type annotation or a initial value for type inference.
Both of followings are legal :
// legal but verbose
var a = MyType()
var b = MyType()
var c = MyType()
// legal but verbose to initialize
var a, b, c: MyType
a = MyType()
b = MyType()
c = MyType()
These two styles I can think of are both legal but somehow verbose, especially if there are dozens of variables of same type.
Is there any elegant way to achieve this?
Upvotes: 12
Views: 19442
Reputation: 300
Some possible solution (maybe worth it if you're planning on doing this several times) is writting a struct to initialize your values and a method (or computed property) on that struct to return the individual components unpacked. You could also add a typealias for improved readability:
typealias MyTypeTrio = (a: MyType, b: MyType, c: MyType)
struct MyTypesPack {
var a, b, c: MyType
init(a: MyType = .init(), b: MyType = .init(), c: MyType = .init()) {
self.a = a
self.b = b
self.c = c
}
var components: MyTypeTrio { (a, b, c) }
}
Usage:
...
let (a, b, c) = MyPackTrio().components
Of course, if you don't need that much granularity of control you may as well just add a static (or not) helper method anywhere (but preferrably in a namespace, such as in an extension of MyType, for instance).
You coud simply write:
extension MyType {
static let trio: MyTypeTrio = (a: MyType(), b: MyType(), c: MyType())
}
and then just use it as:
var (a, b, c) = MyType.trio
I ran into a similar situation recently regarding color components.
Here's what I did then:
typealias RGBA = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
struct RGBAColor {
var red, green, blue, alpha: CGFloat
init(red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0) {
self.red = red
self.green = green
self.blue = blue
self.alpha = alpha
}
var components: RGBA { (red, green, blue, alpha) }
}
Upvotes: 0
Reputation: 1
MyType = MyType() var MyType: [Any] = [Int, "String", "abc", Double, etc..]
Upvotes: -1
Reputation: 184
More smarter way to do this is
let type = (MyType(), MyType(), MyType())
var (a, b, c) = type
Upvotes: -3
Reputation: 38717
Two options in Swift: commas or tuples.
With commas separated statements:
var a = MyType(), b = MyType(), c = MyType()
With a tuple statement:
var (a, b, c) = (MyType(), MyType(), MyType())
Also, but discouraged, you can make multiple statements on one line using semi-colons:
var a = MyType(); var b = MyType(); var c = MyType()
Upvotes: 15
Reputation: 2794
You can declare multiple constants or multiple variables on a single line, separated by commas:
var a = "", b = "", c = ""
NOTE
If a stored value in your code is not going to change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change.
Documentation HERE.
In your case:
var a = MyType(), b = MyType(), c = MyType()
Upvotes: 10