digitalmars.D - aliased functions
- Dan (8/8) Mar 20 2007 Hi guys,
- Carlos Santander (14/27) Mar 20 2007 Something like this?
- Chris Nicholson-Sauls (11/24) Mar 20 2007 Or in other words, its like a function-decleration level interface defin...
- BCS (7/15) Mar 20 2007 You could do something like this.
- janderson (12/25) Mar 20 2007 You could use tuples. Not quite as neat I guess.
- Dan (9/22) Mar 21 2007 I figured out how to do it via mixins, which sorta worked, and slightly ...
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
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
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
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
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
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