| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
March 20, 2007 aliased functions | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
March 20, 2007 Re: aliased functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dan | 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 | |||
March 20, 2007 Re: aliased functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dan | 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
| |||
March 20, 2007 Re: aliased functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Chris Nicholson-Sauls | 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.
| |||
March 21, 2007 Re: aliased functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dan | 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... } | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply