Multi-argument setters

31 views
Skip to first unread message

Mike Austin

unread,
Jun 22, 2012, 1:11:18 AM6/22/12
to magpi...@googlegroups.com
Out of the blue, I just though of a good real-world use for multi-argument setters:

image.image = Image new(named: "foo.png")
button image(UIControlStateNormal) = Image new(named: "foo.png")

I find this more orthogonal than having to use a method that feels like a property (Obj-C):

image.image = [UIImage imageNamed:@"foo.png"];
[button setImage:[UIImage imageNamed:@"foo.png"] forState:UIControlStateNormal];

Mike

Bob Nystrom

unread,
Jun 24, 2012, 12:47:26 PM6/24/12
to magpi...@googlegroups.com
Hey that's clever. This has the advantage that the state can be
treated generically, which methods for each state don't make easy. For
example:

def setImageForState(button, state, image)
button image(state) = image
end

The main use I've come up with for multi-arg setters is just
collections using the [] operator:

list[3] = "value"

This is basically syntactic sugar for calling the setter "[]=" with
list, 3, and value.

Cheers!

- bob
Reply all
Reply to author
Forward
0 new messages