<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#cccccc">
Philippe,<br>
Thank you very much for your response. It looks similar to what
I've done in javascript by wrapping all function arguments into a
single object literal but the D alternative you propose is a little
to convoluted for a beginner like me. Perhaps I'll understand it
better after I'm done reading the D book. <br>
To bad D doesn't support passing arguments by name. It makes code
so much easier to read, especially in large projects. Even Fortran
allows it.<br>
-clk<br>
(Christian Keppenne)<br>
<br>
<blockquote type="cite">
<pre wrap="">On 20/12/2011 14:18, clk wrote:
</pre>
</blockquote>
<pre wrap="">
I remember a discussion about year ago or so.
It seems doable to have some kind of function transformer (adaptor?) for this.
from:
int foo(int a = 0, int b = 1, double c = 0.0, bool d = false) { return 1;}
alias namedParams!foo nfoo; // transform it into a called-by-name function.
nfoo(["d": true]); // a = 0, b = 1, c = 0.0, d = true
nfoo(["d" : true], ["b" : 100]); // a=0, b=100, c=0.0, d=true
nfoo(1, 2, ["d" : true]); // a=1, b=2, c=0.0, d=true
That is, it expects some values, then string/values couples as
associative arrays.
Would that be palatable? Because I think it's doable.
To obtain the arguments names:
int foo(int a, int b, double c = 0.0, bool d = true) { return 1;}
template Name(alias foo) if (isCallable!foo)
{
enum string Name = S!(foo.stringof);
}
template S(string s) // this template is just a trick because
foo.stringof directly displeases DMD
{
enum string S = s;
}
writeln(Name!foo); // "int(int a, int b, double c = 0, bool d = true)"
So this gives us:
- the arguments names
- which ones have default values
- what is that default value
The difficulty here is correctly parsing the ( ,,,) part, without
getting desoriented by argument types that themselves use (,), like
templated types.
I think that would make for an small & interesting community challenge.
Philippe
</pre>
<br>
<blockquote
cite="mid:mailman.6869.1324482902.24801.digitalmars-d-learn@puremagic.com"
type="cite">
<pre wrap="">
End of Digitalmars-d-learn Digest, Vol 71, Issue 34
***************************************************
</pre>
</blockquote>
<br>
</body>
</html>