Passing function parameters by name
bearophile
bearophileHUGS at lycos.com
Tue Dec 4 05:47:17 PST 2007
Artyom Shalkhakov:
> foo( b : 2.5f, a : -3 ); // sort of struct initializer
Python uses =
foo(b=2.5f, a=-3);
This is a very useful feature, I use it often in Python. But if you try to translate this Python code:
from sys import argv
def a(c="A"): print c
def b(c="B"): print c
l = [a, b]
if argv[1] == "1": l.reverse()
l[1]()
to D/C++ you can find a problem, becasue the "A" and "B" must be associated with the function. And the function must know what parameters aren't actually given to it. So you may need to silently pass an array of booleans too (and uint/ulong suffices) that encode what parameters you have actually given to the function (there are other solutions, based on pointers, but they seem even slower to me).
Bye,
bearophile
More information about the Digitalmars-d
mailing list