Jump to page: 1 2
Thread overview
Pseudo member functions
May 12, 2005
Derek Parnell
May 12, 2005
Derek Parnell
May 12, 2005
John Reimer
May 12, 2005
John Reimer
May 12, 2005
Ben Hinkle
May 13, 2005
B.G.
May 13, 2005
David L. Davis
May 13, 2005
Ben Hinkle
May 13, 2005
B.G.
May 13, 2005
pragma
Shift of paradigms
May 14, 2005
Andrew Fedoniouk
May 12, 2005
As you all know, functions of the form ...

   func( char[] s, int i)

can be used in you code as  ...

  char[] x;
  x.func(a);

I'm calling this usage a "pseudo-member function" for want of a better term.

But if the function is defined in a different module, and one needs to disambiguate the function reference, what is the correct syntax for using the function as a pseudo-member function? I know how to call it using traditional syntax ...

   othermod.func(x,a);

but I'm not sure of the other format.

It is not

   x.othermod.func(a);

nor

   x.(othermod.func)(a);

Any ideas anyone?

-- 
Derek
Melbourne, Australia
12/05/2005 12:58:16 PM
May 12, 2005
On Thu, 12 May 2005 13:05:41 +1000, Derek Parnell wrote:

> ... if the function is defined in a different module, and one needs to disambiguate the function reference, what is the correct syntax for using the function as a pseudo-member function?


I have found one form that works!

  with (othermod) x.func(a);

-- 
Derek Parnell
Melbourne, Australia
12/05/2005 2:15:39 PM
May 12, 2005
Derek Parnell wrote:
> As you all know, functions of the form ...
> 
>    func( char[] s, int i) 
> 
> can be used in you code as  ...
> 
>   char[] x;
>   x.func(a);
> 
> I'm calling this usage a "pseudo-member function" for want of a better
> term.
> 
> But if the function is defined in a different module, and one needs to
> disambiguate the function reference, what is the correct syntax for using
> the function as a pseudo-member function? I know how to call it using
> traditional syntax ...
> 
>    othermod.func(x,a);
> 
> but I'm not sure of the other format.
> 
> It is not 
> 
>    x.othermod.func(a);
> 
> nor
> 
>    x.(othermod.func)(a);
> 
> Any ideas anyone?
> 

Good question.  I don't know if there are any slick solutions. I could only think up these possibilities:


# module other;
#
# import std.stdio;
#
# void func( char[] s, int i )
# {
#	writefln("func called: ", s, i);
# }


# module test;
#
# import std.stdio;
# import importable;
#
# void main()
# {
#	char[] x = "Hello";
#	
#	other.func(x, 1);
#	
#	with (other)
#	{
#		x.func(2);
#	}
#
#	x.func(3);
#
#	alias other.func func;
#
# 	x.func(4);
# }

In short, there doesn't seem to be a way using the fully qualified name alone.

-JJR
May 12, 2005
> # module test;
> #
> # import std.stdio;
> # import importable;
> #

Oops! "import importable;" is supposed to be "import other;"
May 12, 2005
"Derek Parnell" <derek@psych.ward> wrote in message news:1hxdwuep1skbe.11d735ffbmq1g.dlg@40tude.net...
> As you all know, functions of the form ...
>
>   func( char[] s, int i)
>
> can be used in you code as  ...
>
>  char[] x;
>  x.func(a);
>
> I'm calling this usage a "pseudo-member function" for want of a better term.

public service announcement for new readers: I vaguely recall from Walter that it was an implementation atrifact and isn't part of the language spec - hence unsupported.


May 13, 2005
Ben Hinkle wrote:
> "Derek Parnell" <derek@psych.ward> wrote in message news:1hxdwuep1skbe.11d735ffbmq1g.dlg@40tude.net...
> 
>>As you all know, functions of the form ...
>>
>>  func( char[] s, int i)
>>
>>can be used in you code as  ...
>>
>> char[] x;
>> x.func(a);
>>
>>I'm calling this usage a "pseudo-member function" for want of a better
>>term.
> 
> 
> public service announcement for new readers: I vaguely recall from Walter that it was an implementation atrifact and isn't part of the language spec - hence unsupported. 
> 
Oh, always wanted to ask about this.

So this 'feature' is considered to be a bug and a bad thing, right?

I haven't written a lot of D programs, but this is a cool feature to write nice code (and to not using classes for strings :-)
This would hurt me if this feature would be dropped. As I've lurked in some forums, this feature is being appreciated by many, so if there's a serious reason to forbid this feature, I'd really want to know why.

Regards!

May 13, 2005
In article <d62jqm$2fd7$1@digitaldaemon.com>, B.G. says...
>
>Ben Hinkle wrote:
>> "Derek Parnell" <derek@psych.ward> wrote in message news:1hxdwuep1skbe.11d735ffbmq1g.dlg@40tude.net...
>> 
>>>As you all know, functions of the form ...
>>>
>>>  func( char[] s, int i)
>>>
>>>can be used in you code as  ...
>>>
>>> char[] x;
>>> x.func(a);
>>>
>>>I'm calling this usage a "pseudo-member function" for want of a better term.
>> 
>> 
>> public service announcement for new readers: I vaguely recall from Walter that it was an implementation atrifact and isn't part of the language spec - hence unsupported.
>> 
>Oh, always wanted to ask about this.
>
>So this 'feature' is considered to be a bug and a bad thing, right?
>
>I haven't written a lot of D programs, but this is a cool feature to
>write nice code (and to not using classes for strings :-)
>This would hurt me if this feature would be dropped. As I've lurked in
>some forums, this feature is being appreciated by many, so if there's a
>serious reason to forbid this feature, I'd really want to know why.
>
>Regards!
>

Walter answered pretty positive about leaving this trick be, in his reply on the 3th of March 2005 found below: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/18038

David L.

P.S. I've started using this trick in my code just recently myself...I really like it a lot!! :)

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
May 13, 2005
> Walter answered pretty positive about leaving this trick be, in his reply
> on the
> 3th of March 2005 found below:
> http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/18038
>
> David L.
>
> P.S. I've started using this trick in my code just recently myself...I
> really
> like it a lot!! :)

Well I'll be darned - it looks like he does say he'll document it. I thought he stepped back from that statement later in the thread but I can't find any reply that says otherwise so it looks like I was mistaken.

-Ben


May 13, 2005
David L. Davis wrote:
> In article <d62jqm$2fd7$1@digitaldaemon.com>, B.G. says...
> 
>>Ben Hinkle wrote:
>>
>>>"Derek Parnell" <derek@psych.ward> wrote in message news:1hxdwuep1skbe.11d735ffbmq1g.dlg@40tude.net...
>>>
>>>
>>>>As you all know, functions of the form ...
>>>>
>>>> func( char[] s, int i)
>>>>
>>>>can be used in you code as  ...
>>>>
>>>>char[] x;
>>>>x.func(a);
>>>>
>>>>I'm calling this usage a "pseudo-member function" for want of a better
>>>>term.
>>>
>>>
>>>public service announcement for new readers: I vaguely recall from Walter that it was an implementation atrifact and isn't part of the language spec - hence unsupported. 
>>>
>>
>>Oh, always wanted to ask about this.
>>
>>So this 'feature' is considered to be a bug and a bad thing, right?
>>
>>I haven't written a lot of D programs, but this is a cool feature to write nice code (and to not using classes for strings :-)
>>This would hurt me if this feature would be dropped. As I've lurked in some forums, this feature is being appreciated by many, so if there's a serious reason to forbid this feature, I'd really want to know why.
>>
>>Regards!
>>
> 
> 
> Walter answered pretty positive about leaving this trick be, in his reply on the
> 3th of March 2005 found below:
> http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/18038
> 
> David L.
> 
> P.S. I've started using this trick in my code just recently myself...I really
> like it a lot!! :)
> 
I love everything looking nice and being efficient.
It's extremely usefull with functions operating on char[]. Using this technique, they are applied like they were methods of some String class, but they're just syntactic sugar, and no string class wrappers are needed. That's is infinitely cool :)

The only thing that itches, are templated functions and functions from another module referenced with the full qualified path.
From earlier threads, for templated functions aliases need to be defined for the whole thing to work, which is IMHO not cool.

I don't know if it's possible, but, if there is a templated function for which we might somehow know that it's template parameter is always supposed to be an _array_, compiler could theoretically be able to choose the 'right' templated function without a need to define an alias.
========

BTW. I've already heared about 5 different names everybody gives to this 'phenomenon'. Is there an official name for this?
is this 'Pseudo member functions' or it's another subjective attempt to give it a name?












May 13, 2005
In article <d62suf$2mrk$1@digitaldaemon.com>, B.G. says...
>
>I don't know if it's possible, but, if there is a templated function for which we might somehow know that it's template parameter is always supposed to be an _array_, compiler could theoretically be able to choose the 'right' templated function without a need to define an alias.


I'm glad you asked this, because I was curious myself.  As it turns out, D uses the rather unintuitive rule "T:T[]" to require an array template type.

> template x(T:T[]){ /* arrays only, T = aggregate type */ }
> template x(T){ /* everything else */ }

Which makes some sense, given that if we want T to assume the actual type passed, we'd use "T[]" or just "T".

Some other handy template rules is use are:

> template x(T:Object){ /* objects only */ }
> template x(T:T*){ /* pointers only, T = aggregate type */ }

Structs are lumped in with scalar types (no common base class), so there's no equivalent for the 'Object' rule above.  Likewise, delegates and functions have no such equivalent, as you need to specify the entire delegate/function type.

> template x(T:struct){ /* won't compile */ }
> template x(T:delegate){ /* won't compile */ }
> template x(T:void delegate()){ /* works (use aliases for cleaner code!) */ }


Anoter tip: Sometimes, I like to declare template sets with rules for types I don't want the user to use under any circumstance.  This goes especially if the template i've defined is too general, and might match types that wouldn't apply. Then I pack the template definition with a "pragma(msg)" along with a "static assert(false)" to warn the unwary developer.  This way, compilation grinds to a halt and the developer is told why.


>
>BTW. I've already heared about 5 different names everybody gives to this
>'phenomenon'. Is there an official name for this?
>is this 'Pseudo member functions' or it's another subjective attempt to
>give it a name?

AFAIK, there is no 'official' name for this feature.  The title 'pseudo member functions' is about as close as we get.  It's definately better than what it really is: 'automatic member-like alias for array types' or an 'outer namespace declared associated member'.

I'm sure the CS crowd out there could come up with a few other more offical-sounding names.

- EricAnderton at yahoo
« First   ‹ Prev
1 2