BookmarkSubscribe

Keyword arguments for functions

I would love if in User Defined funcitons we could use keyword arguments in the calls so I can do

Names default to here(1);
f = function({x, y=2, z=3}, 
	show(x, y, z);
);
print("What I'd like to do");
f(1, z=2);
//I want it to be 1, 2, 2

//instead of having to do 
f = function({x, kwargs={}},
	{DEFAULT LOCAL}, 
	// optional
	y = 2;
	z = 3;
	evallist(kwargs);
	show(x, y, z);	
);
print("Using List");
f(1, {z=2});

Someone mentioned it here, but it was 8 years ago, so figured I'd bring it up again.  

Tracking Number:

Defect ID:

2 Comments
Super User

Your current "kwargs" list solution is a nice interim trick. 

 

 

Super User

Thanks.  It definitely has a few problems. The biggest probably being able to inject any variables into a function and often causes variable collision I'm not careful.  

 

Names default to here(1);

f = function({x, kwargs={}},
	{DEFAULT LOCAL}, 
	// optional
	y = 2;
	z = 3;
	evallist(kwargs);
	print("Before Bad Call");
	show(y);
	print("Bad Call");
	//even though f2 is default local, 
	//because it's not explicit in the function it writes to here
	f2(1, {b=3, y=5}); 
	print("After Bad Call");
	show(y);
);

f2 = function({a, kwargs={}}, 
	{DEFAULT LOCAL}, 
	b=2;
	c=3;
	evallist(kwargs);
	show(a, b, c);
);

f(1, {z=2});