Jump to page: 1 2
Thread overview
return in void functions
Mar 04, 2009
bearophile
Mar 04, 2009
BCS
Mar 04, 2009
Daniel Keep
Mar 05, 2009
grauzone
Mar 05, 2009
Daniel Keep
Mar 05, 2009
Max Samukha
Mar 04, 2009
Walter Bright
Mar 05, 2009
Nick Sabalausky
Mar 05, 2009
BCS
Mar 05, 2009
Yigal Chripun
Mar 08, 2009
Stewart Gordon
Mar 08, 2009
Georg Wrede
Mar 06, 2009
Georg Wrede
Mar 06, 2009
Christopher Wright
March 04, 2009
D has safeties to avoid this kind of code, because it's essentially considered a bug:

int foo() {}
void main() {
    foo();
}


But this compiles with no errors:

void foo() {
    return 1;
}
void main() {
    foo();
}


Notice that this Java code:

class Test {
	static void foo() {
		return 1;
	}

    public static void main(String[] args) {
		Test.foo();
    }
}

Raises a compilation error:

Test.java:3: cannot return a value from method whose result type is void
                return 1;
                       ^
1 error

I think this avoids some mistakes.

Bye,
bearophile
March 04, 2009
Reply to bearophile,


> But this compiles with no errors:
> 
> void foo() {
> return 1;
> }
> void main() {
> foo();
> }

IIRC D allows "return exp;" in a void function because it avoids special cases in template code.

ReturnTypeOf!(AFn, T) fn(T)(T t)
{
   return AFn(t,t); // AFn might return void
}


March 04, 2009

BCS wrote:
> Reply to bearophile,
> 
> 
>> But this compiles with no errors:
>>
>> void foo() {
>> return 1;
>> }
>> void main() {
>> foo();
>> }
> 
> IIRC D allows "return exp;" in a void function because it avoids special cases in template code.
> 
> ReturnTypeOf!(AFn, T) fn(T)(T t)
> {
>    return AFn(t,t); // AFn might return void
> }

Sadly, this doesn't help if you need to cache the result before exiting.
 I personally wish we could create void *variables*.

  -- Daniel
March 04, 2009
BCS wrote:
> IIRC D allows "return exp;" in a void function because it avoids special cases in template code.
> 
> ReturnTypeOf!(AFn, T) fn(T)(T t)
> {
>    return AFn(t,t); // AFn might return void
> }

Yes, that's exactly why.
March 05, 2009
"Walter Bright" <newshound1@digitalmars.com> wrote in message news:gomvoo$1m3b$3@digitalmars.com...
> BCS wrote:
>> IIRC D allows "return exp;" in a void function because it avoids special cases in template code.
>>
>> ReturnTypeOf!(AFn, T) fn(T)(T t)
>> {
>>    return AFn(t,t); // AFn might return void
>> }
>
> Yes, that's exactly why.

There has to be a better way to handle that.  Making "void foo(){return 1;}" valid code with no warning is just plain sloppy. (In fact, I've been bitten by that before.)


March 05, 2009
Hello Nick,


> There has to be a better way to handle that.  Making "void
> foo(){return 1;}" valid code with no warning is just plain sloppy. (In
> fact, I've been bitten by that before.)
> 

I wouldn't mind seeing it limited to "return FnReturningVoid();"


March 05, 2009
Nick Sabalausky wrote:
> "Walter Bright" <newshound1@digitalmars.com> wrote in message news:gomvoo$1m3b$3@digitalmars.com...
>> BCS wrote:
>>> IIRC D allows "return exp;" in a void function because it avoids special cases in template code.
>>>
>>> ReturnTypeOf!(AFn, T) fn(T)(T t)
>>> {
>>>    return AFn(t,t); // AFn might return void
>>> }
>> Yes, that's exactly why.
> 
> There has to be a better way to handle that.  Making "void foo(){return 1;}" valid code with no warning is just plain sloppy. (In fact, I've been bitten by that before.) 
> 
> 

I agree. It's fine to forward a void to a void, but not anything else to a void.


Andrei
March 05, 2009
Walter Bright wrote:
> BCS wrote:
>> IIRC D allows "return exp;" in a void function because it avoids
>> special cases in template code.
>>
>> ReturnTypeOf!(AFn, T) fn(T)(T t)
>> {
>> return AFn(t,t); // AFn might return void
>> }
>
> Yes, that's exactly why.

D is becoming more functional and the functional way is to use a unit type. why not turn D's void from C style keyword to a unit type?
March 05, 2009
Daniel Keep wrote:
> 
> BCS wrote:
>> Reply to bearophile,
>>
>>
>>> But this compiles with no errors:
>>>
>>> void foo() {
>>> return 1;
>>> }
>>> void main() {
>>> foo();
>>> }
>> IIRC D allows "return exp;" in a void function because it avoids special
>> cases in template code.
>>
>> ReturnTypeOf!(AFn, T) fn(T)(T t)
>> {
>>    return AFn(t,t); // AFn might return void
>> }
> 
> Sadly, this doesn't help if you need to cache the result before exiting.
>  I personally wish we could create void *variables*.

I wonder if we'd lose anything if the void datatype was completely removed and replaced by a "struct void {}" declaration in object.d?

>   -- Daniel
March 05, 2009

grauzone wrote:
> Daniel Keep wrote:
>>
>> BCS wrote:
>>> Reply to bearophile,
>>>
>>>
>>>> But this compiles with no errors:
>>>>
>>>> void foo() {
>>>> return 1;
>>>> }
>>>> void main() {
>>>> foo();
>>>> }
>>> IIRC D allows "return exp;" in a void function because it avoids special cases in template code.
>>>
>>> ReturnTypeOf!(AFn, T) fn(T)(T t)
>>> {
>>>    return AFn(t,t); // AFn might return void
>>> }
>>
>> Sadly, this doesn't help if you need to cache the result before exiting.
>>  I personally wish we could create void *variables*.
> 
> I wonder if we'd lose anything if the void datatype was completely removed and replaced by a "struct void {}" declaration in object.d?
> 
>>   -- Daniel

T x = void;

Interesting idea, though.

  -- Daniel
« First   ‹ Prev
1 2