Jump to page: 1 2
Thread overview
[Issue 8106] New: func.stringof with default args
May 16, 2012
Manu
May 16, 2012
Manu
Jun 12, 2012
Walter Bright
Jun 13, 2012
Manu
Jun 13, 2012
Walter Bright
Jun 13, 2012
Manu
Jun 14, 2012
Walter Bright
Jun 14, 2012
Walter Bright
Jun 14, 2012
Walter Bright
Jun 14, 2012
Walter Bright
Jun 14, 2012
Walter Bright
Jun 14, 2012
Manu
Jun 14, 2012
Walter Bright
Jun 14, 2012
Manu
Jun 15, 2012
Kenji Hara
Jun 26, 2012
Walter Bright
Jun 26, 2012
Kenji Hara
Jun 26, 2012
Manu
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106

           Summary: func.stringof with default args
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: turkeyman@gmail.com


--- Comment #0 from Manu <turkeyman@gmail.com> 2012-05-16 00:54:13 PDT ---
func.stringof is the only existing way to deeply introspect function calling information past it's argument types (I need argument names, default arguments), so I rely on this very heavily.

There are some annoying cases where it doesn't work as expected though:

struct Colour
{
  ubyte a,r,g,b;

  immutable Colour white = Colour(255,255,255,255);
}

void func(Colour c = Colour.white)

func.stringof == "void func(Colour c = white)"
Error: undefined identifier white

For some reason the scope Colour. was removed from the initialiser.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106


Manu <turkeyman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |major


--- Comment #1 from Manu <turkeyman@gmail.com> 2012-05-16 01:28:28 PDT ---
I raised the priority a notch. I have no workaround for this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-06-12 16:46:31 PDT ---
For this program:
=======================
struct Colour
{
  ubyte a,r,g,b;

  immutable Colour white = Colour(255,255,255,255);
}

void func(Colour c = Colour.white);

void main()
{
   pragma(msg, func.stringof);
}
=======================
it prints:

  func(white)

not:

  void func(Colour c = white)

Can you please verify the behavior you are seeing?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #3 from Manu <turkeyman@gmail.com> 2012-06-12 17:24:20 PDT ---
(In reply to comment #2)
> Can you please verify the behavior you are seeing?

Sorry, that should have a typeof():

pragma(msg, typeof(func).stringof);
-> void(Colour c = cast(Colour)white)

I don't recall it showing the cast that way at work (old GDC differences
perhaps), but that's what I'm seeing right now.
Either way, that should surely read c = Colour.white instead.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2012-06-13 14:55:42 PDT ---
I have a patch for this specific case, but it's a kludge, not a general solution.

The problem is that the context of the typeof(x) is not the context of where x
was declared, hence it is unknown what scope qualifiers (such as Colour) need
to be applied.

Can you post an example of why you need this? Perhaps there's another way.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #5 from Manu <turkeyman@gmail.com> 2012-06-13 15:50:11 PDT ---
(In reply to comment #4)
> Can you post an example of why you need this? Perhaps there's another way.

It's difficult for me to extract a concrete example (a very large context, in work code), but essentially I'm creating functions/methods that match (wrap) user declared function pointers, which include default args in the declarations. The default args need to propagate to the magic wrappers.

There is no mechanism to copy a parameter list from one function type to another (with names + default args), nor are there traits to get the argument names, or the default arg expression, so I can only construct declarations for mixins by parsing that string.


An alternative approach I can use (prefer) is to invert the problem, allowing
the user to define function prototypes (rather than function pointers), and
then procedurally produce the function body. Although this conflicts with the
limitation I posted in bug 8108, where you can't forward declare/prototype a
function in a module/class, and then define it later in the same scope.
The prototype would be declared by a user, and a later mixin would scan for
prototypes and produce the function bodies within the same module.

// user declares:
void Function(int x = 10); // note: default args are still important

// and using magic, mixin:
void Function(int x)
{
  // generated body..
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> 2012-06-13 20:05:42 PDT ---
Ok, I suggest abandoning this .stringof proposal as unworkable. Instead, how about a traits that gives the default arguments as an expression tuple?

Also, the argument names shouldn't be necessary, you can just generate your own.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2012-06-13 20:50:27 PDT ---
Suppose I made this work:
-------------------------
template ParameterTypeTuple(alias foo)
{
    static if (is(typeof(foo) P == function))
        alias P ParameterTypeTuple;
    else
        static assert(0, "argument has no parameters");
}

int func(int i, long j = 7) { return 3; }

alias ParameterTypeTuple!func PT;

int bar(PT) { return 4; }

pragma(msg, typeof(bar));

void main()
{
    bar(1);
}
----------------------------

I.e. the default argument is propagated to the tuple. Will that work for you?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #8 from Walter Bright <bugzilla@digitalmars.com> 2012-06-13 22:23:21 PDT ---
Note that the default argument can be retrieved using something like:

PT[1] getDefault(PT a) { return a[1]; }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 14, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8106



--- Comment #9 from Walter Bright <bugzilla@digitalmars.com> 2012-06-13 22:35:21 PDT ---
Or better:

PT[1] getDefault(PT[1..2] a) { return a[0]; }

An example:
-----------------
import std.stdio;

template ParameterTypeTuple(alias foo)
{
    static if (is(typeof(foo) P == function))
        alias P ParameterTypeTuple;
    else
        static assert(0, "argument has no parameters");
}

int func(int i, long j = 7) { return 3; }

alias ParameterTypeTuple!func PT;

int bar(PT) { return 4; }

pragma(msg, typeof(bar));
pragma(msg, PT[1]);

PT[1] boo(PT[1..2] a) { return a[0]; }

void main()
{
    writeln(boo());
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2