I think this should be allowed and considered valid in all languages. If you have a function whose return type is void , and you return the result of an expression whose type is void (for example, calling another void function), you are satisfied with this restriction.
It is not considered useful to do this in C (although I think it can be allowed) because there is no reason for this. Anytime:
return someVoidFn();
You can always translate this:
someVoidFn(); return;
And get the same effect.
However, in C ++ and Java, returning the void function really makes sense, and that is because these languages ββhave templates and generics. Consider this (not very useful) Java class:
class NestedIdentity<T> { T run(int i, T value) { if (i == 0) return value; return run(i - 1); } }
His run method returns the value that you pass to it after calling yourself a certain number of times. I know, pointless, right? But the important part is that T can be invalid. You can do it:
NestedIdentity<Void> nest = new NestedIdentity<Void>(); nest(5, null);
And it works. In Java, void (note capitalization) creates an instance of type void whose only value is null . If Java did not allow the return of the void expression in the void method, this should have been a compile-time error to create an instance using void.
source share