[swift-evolution] [Proposal] Property behaviors

Félix Cloutier felixcca at yahoo.ca
Wed Jan 13 21:13:23 CST 2016


I started by reading the examples and I was very confused. This suggests to me that if you've never seen a var behavior before, you are going to wonder what the hell is going on. :-)

Notable points of confusion:

it's confusing to me that `self` is the containing type and the behavior name is the "behavior's self".
The `initializer` special field feels absolutely magic. Has anything else been considered, like an init param that has either a Value or an autoclosure returning one? (If we don't want to capture self, aren't we in for problems capturing self from accessors anyway?)
I see (after reading it) that `var behavior foo<Value>: Value` means that foo "applies to"/"wraps" Value, but I find it confusing to use a syntax more typically associated with "extends" or "implements" or "is a".

Questions:

Can a behavior have generic parameters that can't be inferred? Could I write, say, [fooable<Int>]?
What is the tradeoff between `eager` and `deferred`? Is it "only" that `deferred` side effects happen at the mercy of the behavior?
If so, isn't it a problem that behaviors aren't intrinsically explicit about whether they defer initialization? I can see that causing very subtle bugs.

Concerns:

It looks like if you had a [resettable, observable] property, calling resettable.reset() would change the value from under `observable`'s feet.

Comments:

While it might be true that square brackets work better with other declarations that could eventually have behaviors, "var behavior" doesn't really lend itself to that kind of extensibility. Are we steering towards "func behavior", "class behavior", etc? Is it a problem if we are?
I'd like to point out that the memoization example is a let variable with a behavior, which is explicitly forbidden by the current proposal.

Finally, I would like to throw the idea of "foo..resettable" to access foo's resettable behavior (or foo..reset() doing optionally-qualified lookup on foo's behavior methods).

Félix

> Le 13 janv. 2016 à 17:07:06, Joe Groff via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> Thanks everyone for the first round of feedback on my behaviors proposal. I've revised it with the following changes:
> 
> - Instead of relying on mapping behaviors to function or type member lookup, I've introduced a new purpose-built 'var behavior' declaration, which declares the accessor and initializer requirements and provides the storage and behavior methods of the property. I think this gives a clearer design for authoring behaviors, and allows for a more efficient and flexible implementation model.
> - I've backed off from trying to include 'let' behaviors. As many of you noted, it's better to tackle immutable computed properties more holistically than to try to backdoor them in.
> - I suggest changing the declaration syntax to use a behavior to square brackets—'var [behavior] foo'—which avoids ambiguity with destructuring 'var' bindings, and also works with future candidates for behavior decoration, particularly `subscript`.
> 
> Here's the revised proposal:
> 
> https://gist.github.com/jckarter/50b838e7f036fe85eaa3
> 
> For reference, here's the previous iteration:
> 
> https://gist.github.com/jckarter/f3d392cf183c6b2b2ac3
> 
> Thanks for taking a look!
> 
> -Joe
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/8d9e87a4/attachment.html>


More information about the swift-evolution mailing list