| Thread overview | |||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 04, 2009 return in void functions | ||||
|---|---|---|---|---|
| ||||
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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS |
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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | "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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | 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 Re: return in void functions | ||||
|---|---|---|---|---|
| ||||
Posted in reply to grauzone |
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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply