This may just be me, but I am having a lot of trouble digesting this example:
What is happening here!? Is x
being captured? Is x
the name of a stored property in the struct being created? Wherefore art thou x
? Also, I am immediately perplexed by what var = x + 1
means (the var =
without the label is really throwing me off). Is that actually referring to the y
property requirement of Foo
and if so, does eq.y
always return eq.x + 1
, or is it just initial set to the variable x
that is not a member of eq
+ 1? So many questions, yet so little code! At first glance, all of these things are unclear and honestly, a bit more than that, but let me try to take a stab at this (please correct me if I'm wrong, I'm having a decent amount of trouble interpreting this).
From what I understand from the nature of this proposal, eq
is desugared into the following:
struct _Anonymous: Foo, Equatable {
private(set) var x: Int \\ or let?
var y: Int
}
let x = 0
let eq = _Anonymous(x: x, y: x + 1)
Please correct me if my understanding is incorrect. If I was indeed able to untangle that example, I think you should definitely change the let x = 0
to some other variable name, otherwise you are just sorta asking for confusion.
Also, I feel like it makes it much more unclear not requiring the argument names as when you look at eq
it is not really clear how it satisfies the requirements of Foo
without going through the order of the property requirements themselves. Also, if Foo
happened to be in a different file, knowing that var =
is actually referring to the y
property is not at all immediately obvious to someone reading the code. As of now, all types (classes, structs) need to write the name of their stored properties in their implementations, not having the same requirement on anonymous structures seems quite confusing to me. For example, if I suddenly stumbled into this in code I'm reading, I would be fairly perplexed:
let x = 0
var eq: some Equatable & Foo = { [x, var = x + 1] }
eq.y = 7 // Where did that property come from?
That being said, I feel like the following would be much clearer:
protocol Foo {
var x: Int { get }
var y: Int { get set }
}
let a = 0
let eq: some Equatable & Foo = { [x = a, var y = a + 1] }
If I totally missed the mark here, please feel free to correct me. I like the idea of this feature a lot, but the syntax is really throwing me off.