Named arguments by themselves do not fundamentally prevent data type errors when the underlying types are identical. Short variable names (v1, v2) or some other cosmetic ambiguity will deceptively compile with named arguments as the only defense.
Both of the following would compile successfully using named arguments but one of them is semantically wrong:
Player(pos = v1, size = v2);
Player(pos = v2, size = v1);
Using a compiler's static type checking enforces correct semantics more than named arguments. Of course, this only works if the programmer creates new differentiated types so that they are no longer identical (which was the crux of the article.)
Re:Let the Type System do the Work
by TheLoneWolfling on Monday April 21, 2014 @02:25PM
(#7622692)
I'd disagree with you: named arguments are good, but they don't save you in a lot of cases.
For example when you have one function returning (pos, size) and another function expecting (size, pos).
Re:Let the Type System do the Work
by hcarvalhoalves on Monday April 21, 2014 @02:40PM
(#7622798)
Named arguments is syntatic sugar for packing/unpacking data structures in some languages, so you can leverage that. In Python:
>>> from collections import namedtuple
>>> Vector2 = namedtuple('Vector2', ('x', 'y'))
>>> PlayerOptions = namedtuple('PlayerOptions', ('position', 'size'))
>>> opt = PlayerOptions(position=Vector2(100, 100), size=Vector2(50, 50))
>>> Player(**opt._asdict())
The order arguments are passed doesn't matter anymore. You can also enforce this calling convention with a constructor signature like this (in 3): >>> class Player(object):
>>> def __init__(self, *, size, position):i agree that in languages without compiler defined types, but with other tools such as named arguments, the class of errors described here can be to some extent avoided by making good use of named arguments. I'm gonna do this more often in my python code!
Re:Let the Type System do the Work
by felipehummel on Monday April 21, 2014 @10:04PM
(#7625353)
Although named arguments are good, you won't use it in all function calls.
> No problems here. The first noticible crack in the system comes after a few weeks vacation away from this code. You come back, and you want to change the starting point of the player. Will you remember which Vector2 to change?
The problem is not typing here, it's lack of named arguments.