Is returning void functions inside void functions a feature or an artifact?
jfondren
julian.fondren at gmail.com
Mon Aug 2 14:46:36 UTC 2021
On Monday, 2 August 2021 at 14:31:45 UTC, Rekel wrote:
> I recently found one can return function calls to void
> functions, though I don't remember any documentation mentioning
> this even though it doesn't seem trivial.
>
> ```d
> void print(){
> writeln("0");
> }
>
> void doSomething(int a){
> if (a==0)
> return print();
> writeln("1");
> }
>
> void main(string[] args) {
> doSomething(0); // Prints 0 but not 1.
> }
> ```
>
> If this is intended, where could I find this in the docs? I
> haven't been able to find previous mentions on this, neither on
> the forum.
I don't know where you can find this in the docs, but what
doesn't seem trivial about it? The type of the expression
`print()` is void. That's the type that `doSomething` returns.
That's the type of the expression that `doSomething` does return
and the type of the expression following a `return` keyword in
`doSomething`. Rather than a rule expressly permitting this, I
would expect to find to either nothing (it's permitted because it
makes sense) or a rule against it (it's expressly forbidden
because it has to be to not work, because it makes sense).
C, C++, Rust, and Zig are all fine with this. Nim doesn't like it.
More information about the Digitalmars-d-learn
mailing list