Thread overview | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 22, 2004 void returns | ||||
---|---|---|---|---|
| ||||
Hello, Would it be possible to add support for void returns, as in void f1 () { ... } void f2 () { ...; return f1 (); } to D? This would be invaluably helpful in template programming. Else one has to either make heavy use of mixins or write a lot of specialisations. (Looking at the code, it seems, that only dmd/statement.c has to be modified to archive this goal). regards, Benedikt -- NetBSD Operating system: http://www.NetBSD.org/ pkgsrc "Work in progress": http://pkgsrc-wip.sf.net/ XFce desktop environment: http://www.xfce.org/ German Unix-AG Association: http://www.unix-ag.org/ os-network: http://www.os-network.de/ OpenPGP Key: http://www.home.unix-ag.org/bmeurer/#gpg |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benedikt Meurer | Benedikt Meurer wrote: > Hello, > > Would it be possible to add support for void returns, as in > > void f1 () { ... } > void f2 () { ...; return f1 (); } > > to D? This would be invaluably helpful in template programming. Else one has to either make heavy use of mixins or write a lot of specialisations. > > (Looking at the code, it seems, that only dmd/statement.c has to be modified to archive this goal). > > regards, > Benedikt In D, void means the function doesn't return a value. But that doesn't prevent you from calling another function. I'm not sure I understand what your goal is. You can already do this: void f1() { printf("Running f1...\n"); } void f2() { bit itsAGoodIdea = true; printf("Running f2...\n"); if(itsAGoodIdea) { f1(); return; } printf("Don't go there.\n"); } void main() { f2(); printf("The end.\n"); } /* Output: Running f2... Running f1... The end. */ > > -- > NetBSD Operating system: http://www.NetBSD.org/ > pkgsrc "Work in progress": http://pkgsrc-wip.sf.net/ > XFce desktop environment: http://www.xfce.org/ > German Unix-AG Association: http://www.unix-ag.org/ > os-network: http://www.os-network.de/ > > OpenPGP Key: http://www.home.unix-ag.org/bmeurer/#gpg -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/ |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | J C Calvarese wrote:
> In D, void means the function doesn't return a value. But that doesn't prevent you from calling another function. I'm not sure I understand what your goal is.
Right. He's referring to syntactically allowing 'void' to be returned.
void f1() { }
void f2() { return f1(); }
f1 has no return value, and neither does f2, so it... sort of, makes sense. The big reason to allow it is because the return type may be parameterized:
template t(T) {
T f1() { ... }
T f2() { return f1(); }
}
If void can be returned, T can be void, in this example.
-- andy
|
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | Andy Friesen wrote: > J C Calvarese wrote: > >> In D, void means the function doesn't return a value. But that doesn't prevent you from calling another function. I'm not sure I understand what your goal is. > > > Right. He's referring to syntactically allowing 'void' to be returned. > > void f1() { } > void f2() { return f1(); } > > f1 has no return value, and neither does f2, so it... sort of, makes sense. The big reason to allow it is because the return type may be parameterized: > > template t(T) { > T f1() { ... } > T f2() { return f1(); } > } > > If void can be returned, T can be void, in this example. > > -- andy Oops. Somehow I missed his reference to templates (perhaps my eyes started to glaze after after I saw his example). Sounds like a good idea since it'd help templatization. -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/ |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benedikt Meurer | Ah, something I've always wanted in C++... I echo the request. "Benedikt Meurer" <benedikt.meurer@unix-ag.uni-siegen.de> wrote in message news:c8o52s$1j0l$1@digitaldaemon.com... > Hello, > > Would it be possible to add support for void returns, as in > > void f1 () { ... } > void f2 () { ...; return f1 (); } > > to D? This would be invaluably helpful in template programming. Else one has to either make heavy use of mixins or write a lot of specialisations. > > (Looking at the code, it seems, that only dmd/statement.c has to be modified > to archive this goal). > > regards, > Benedikt > > -- > NetBSD Operating system: http://www.NetBSD.org/ pkgsrc "Work in progress": http://pkgsrc-wip.sf.net/ XFce desktop environment: http://www.xfce.org/ German Unix-AG Association: http://www.unix-ag.org/ os-network: http://www.os-network.de/ > > OpenPGP Key: http://www.home.unix-ag.org/bmeurer/#gpg |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | works in c++... should be working in D, too, indeed. "Matthew" <matthew.hat@stlsoft.dot.org> schrieb im Newsbeitrag news:c8okjt$28o9$1@digitaldaemon.com... > Ah, something I've always wanted in C++... > > I echo the request. > > "Benedikt Meurer" <benedikt.meurer@unix-ag.uni-siegen.de> wrote in message news:c8o52s$1j0l$1@digitaldaemon.com... > > Hello, > > > > Would it be possible to add support for void returns, as in > > > > void f1 () { ... } > > void f2 () { ...; return f1 (); } > > > > to D? This would be invaluably helpful in template programming. Else one has > > to either make heavy use of mixins or write a lot of specialisations. > > > > (Looking at the code, it seems, that only dmd/statement.c has to be modified > > to archive this goal). > > > > regards, > > Benedikt > > > > -- > > NetBSD Operating system: http://www.NetBSD.org/ pkgsrc "Work in progress": http://pkgsrc-wip.sf.net/ XFce desktop environment: http://www.xfce.org/ German Unix-AG Association: http://www.unix-ag.org/ os-network: http://www.os-network.de/ > > > > OpenPGP Key: http://www.home.unix-ag.org/bmeurer/#gpg > > |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to davepermen | Yes, but it's a relatively late addition, and several recent or still in use compilers don't support it, which makes its use a portability problem. Here's another one to consider. In C++ I've had occasion to require a sizeof(void), and had to resort to traits in order to get it. Does anyone see a downside with sizeof(void) being legal in D? "davepermen" <davepermen@hotmail.com> wrote in message news:c8ol00$29c3$1@digitaldaemon.com... > works in c++... > > should be working in D, too, indeed. > > "Matthew" <matthew.hat@stlsoft.dot.org> schrieb im Newsbeitrag news:c8okjt$28o9$1@digitaldaemon.com... > > Ah, something I've always wanted in C++... > > > > I echo the request. > > > > "Benedikt Meurer" <benedikt.meurer@unix-ag.uni-siegen.de> wrote in message news:c8o52s$1j0l$1@digitaldaemon.com... > > > Hello, > > > > > > Would it be possible to add support for void returns, as in > > > > > > void f1 () { ... } > > > void f2 () { ...; return f1 (); } > > > > > > to D? This would be invaluably helpful in template programming. Else one > has > > > to either make heavy use of mixins or write a lot of specialisations. > > > > > > (Looking at the code, it seems, that only dmd/statement.c has to be > modified > > > to archive this goal). > > > > > > regards, > > > Benedikt > > > > > > -- > > > NetBSD Operating system: http://www.NetBSD.org/ pkgsrc "Work in progress": http://pkgsrc-wip.sf.net/ XFce desktop environment: http://www.xfce.org/ German Unix-AG Association: http://www.unix-ag.org/ os-network: http://www.os-network.de/ > > > > > > OpenPGP Key: http://www.home.unix-ag.org/bmeurer/#gpg > > > > > > |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | I suppose sizeof(void) would need to reflect the size of void[] elements, which are 1 byte each ... "Matthew" <matthew.hat@stlsoft.dot.org> > Here's another one to consider. In C++ I've had occasion to require a sizeof(void), and had to resort to traits in order to get it. Does anyone see a > downside with sizeof(void) being legal in D? |
May 22, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benedikt Meurer | Benedikt Meurer wrote:
> Hello,
>
> Would it be possible to add support for void returns, as in
>
> void f1 () { ... }
> void f2 () { ...; return f1 (); }
According to Bertrand Meyer, creator of Eiffel, there are two kinds of routines:
Commands (i.e. functions that return void), and
Queries (i.e. functions that return non-void).
According to Meyer, Commands should be used to change state, and Queries should be used only to query that state.
What is the point of this distinction? For a variety of reasons, the most important being exception-safety, Queries should not have side-effects (roughly, they should not change state). This is why pop() doesn't return a value in the STL stack. pop() is the Command, and top() is the Query.
If functions that return void and functions that return non-void should be used for fundamentally different purposes, when would it be a good idea to have function template parameterized to sometimes return void and sometimes return non-void?
James McComb
|
May 23, 2004 Re: void returns | ||||
---|---|---|---|---|
| ||||
Posted in reply to James McComb | <snip>
>According to Meyer, Commands should be used to change state, and Queries should be used only to query that state.
</snip>
Well then I submit that a knife is to cut food, and a fork is to pick up food (with a spoon being a specialized type of fork?). So one should not use a fork to cut pancakes.
|
Copyright © 1999-2021 by the D Language Foundation