March 05, 2009
On Thu, 05 Mar 2009 08:40:34 +0100, grauzone <none@example.net> 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?

That would be awesome. Would foo() and foo(void) be distinct overloads, one taking no arguments and the other taking a void argument? Should voids be allowed to be declared directly or only passed to template parameters?

>
>>   -- Daniel
March 06, 2009
bearophile wrote:
> 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();
> }

No error in C either, only a warning.

This being an easy thing to fix, one would think it'd been fixed ages ago already. Therefore, one only assumes there to be a motivation for having it like this.
March 06, 2009
bearophile wrote:
> D has safeties to avoid this kind of code, because it's essentially considered a bug:
> 
> But this compiles with no errors:
> 
> void foo() {
>     return 1;
> }
> void main() {
>     foo();
> }

This is an error you'll catch very quickly. If you never use the function, you won't have an error, which is a problem if you're releasing a library that you did not test.
March 08, 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.

Why it allows you to return an int from a void-returning function???

Stewart.
March 08, 2009
Stewart Gordon wrote:
> 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.
> 
> Why it allows you to return an int from a void-returning function???

C does that, too.

void foo()
{
    return 23;
}

int  main()
{
    foo();
}

Running the program gives random values as the exit status.
Changing void to int returns always 23.

(Actually I would have thought that it'd give 23 in both cases.
But I guess the assignment of 23 into the return register doesn't
get compiled when void is used.)

----

Anyway, I don't think this is such a big deal. Fixing this may be a lot of work, and I haven't heard anyone complain about it, in C or D, before.

It's a blemish, but one we can live with.
1 2
Next ›   Last »