www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - aliased functions

reply Dan <murspoft hotmail.com> writes:
Hi guys,

I was just going through my huge methods file, and I thought to myself that it
might be beneficial if we somehow allowed ourselves to use:

alias int function(int x, uint y) myFunctionStandard;

myFunctionStandard myFunctionName {
 functionContents...
}

Reasons?  Keeping it all in one place could reduce errors, make it more
readable, and less verbose.  I have a file with over 180 methods that all have
identical parameters so they can be called through a common mechanism.

Alternatively, anyone have a template for this already?
Mar 20 2007
next sibling parent Carlos Santander <csantander619 gmail.com> writes:
Dan escribió:
 Hi guys,
 
 I was just going through my huge methods file, and I thought to myself that it
might be beneficial if we somehow allowed ourselves to use:
 
 alias int function(int x, uint y) myFunctionStandard;
 
 myFunctionStandard myFunctionName {
  functionContents...
 }
 
 Reasons?  Keeping it all in one place could reduce errors, make it more
readable, and less verbose.  I have a file with over 180 methods that all have
identical parameters so they can be called through a common mechanism.
 
 Alternatively, anyone have a template for this already?
Something like this? alias int function (int x, uint y) myFunctionStandard; int foo (int a, uint b) { return 0; } myFunctionStandard myFunctionName () { return &foo; } It's legal D, btw. -- Carlos Santander Bernal
Mar 20 2007
prev sibling next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Dan wrote:
 Hi guys,
 
 I was just going through my huge methods file, and I thought to myself that it
might be beneficial if we somehow allowed ourselves to use:
 
 alias int function(int x, uint y) myFunctionStandard;
 
 myFunctionStandard myFunctionName {
  functionContents...
 }
 
 Reasons?  Keeping it all in one place could reduce errors, make it more
readable, and less verbose.  I have a file with over 180 methods that all have
identical parameters so they can be called through a common mechanism.
 
 Alternatively, anyone have a template for this already?
Or in other words, its like a function-decleration level interface definition, yes? Actually I have wanted something like this from time to time. For example, in one project I have a module with about fifteen functions with the exact same signature, aside from name. And several other modules, all containing bundles of functions, and all of them with that very same signature. Suppose I extend that signature in a later revision... Or suppose I typo while implementing yet another... Or suppose a lot of things. It would've been very handy to have something like this to keep me on track. I may not be fond of languages that hold my hand, but I am fond of languages that help me hold my own. :) -- Chris Nicholson-Sauls
Mar 20 2007
parent BCS <ao pathlink.com> writes:
Reply to Chris Nicholson-Sauls,

 Suppose I extend that signature
 in a later revision...  Or suppose I typo while implementing yet
 another...  Or suppose a lot of things.  It would've been very handy
 to have something like this to keep me on track.
 
 I may not be fond of languages that hold my hand, but I am fond of
 languages that help me hold my own.  :)
 
You could do something like this. typedef int function(int) callback int foo(int bar){...} static assert(is(&foo : callback)); It won't save you typing, but it will find errors. I'd be interested in the "protocol" idea as well.
Mar 20 2007
prev sibling next sibling parent janderson <askme me.com> writes:
Dan wrote:
 Hi guys,
 
 I was just going through my huge methods file, and I thought to myself that it
might be beneficial if we somehow allowed ourselves to use:
 
 alias int function(int x, uint y) myFunctionStandard;
 
 myFunctionStandard myFunctionName {
  functionContents...
 }
 
 Reasons?  Keeping it all in one place could reduce errors, make it more
readable, and less verbose.  I have a file with over 180 methods that all have
identical parameters so they can be called through a common mechanism.
 
 Alternatively, anyone have a template for this already?
You could use tuples. Not quite as neat I guess. template Tuple(E...) { alias E Tuple; } alias Tuple!(int, uint) myFunctionStandard; alias int myFunctionStandardReturn; myFunctionStandardReturn myFunctionName(myFunctionStandard tl) { functionContents... }
Mar 20 2007
prev sibling parent Dan <murpsoft hotmail.com> writes:
janderson Wrote:

 You could use tuples.  Not quite as neat I guess.
 
 template Tuple(E...)
 {
      alias E Tuple;
 }
 alias Tuple!(int, uint) myFunctionStandard;
 alias int myFunctionStandardReturn;
 
 myFunctionStandardReturn myFunctionName(myFunctionStandard tl)
 {
   functionContents...
 }
I figured out how to do it via mixins, which sorta worked, and slightly less syntax than the tuple. However, Carlos suggested you already can do it via the alias: alias int function(int x, uint y) myF; myF bob { return 0; } ^^ That's what I remember of it. *that* is the solution I wanted, and it actually reduces the possibility of errors, cuts redundancy and simplifies it. Thanks Carlos. : )
Mar 21 2007