Thread overview | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 04, 2012 Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Attachments:
| So here's some problems. I use 'const ref' to pass structs to functions (note: because 'in ref' doesn't seem to work) And I often need to return structs by ref too, but I'm having problems: void test( const ref Thing x ) {} // this works fine. note, 'const ref' works fine here (in lieu of 'in ref') struct Thing { } Thing thing; const ref Thing func() { return gThing; } This syntax complains, but it's precisely the same expression I use to pass an argument in to a function, and it's fine there: remedy\modules\hud.d(35):Error: function remedy.hud.func without 'this' cannot be const/immutable const ref Thing function() blah = &func; I need to take a pointer to this function, but It complains when I declare a 'function' of this type: remedy\modules\hud.d(36):Error: variable remedy.hud.blah only parameters or foreach declarations can be ref (... what? it works for parameters?) I try rearranging the syntax to make the first issue stop complaining: ref const(Thing) func2() { return gThing; } // this seems to work now, but i don't like the inconsistency... ref const(Thing) function() blah2 = &func; Error: variable remedy.hud.blah2 only parameters or foreach declarations can be ref |
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 2012-05-04 10:38, Manu wrote: > So here's some problems. > > I use 'const ref' to pass structs to functions (note: because 'in ref' > doesn't seem to work) > And I often need to return structs by ref too, but I'm having problems: > > void test( const ref Thing x ) {} // this works fine. note, 'const ref' > works fine here (in lieu of 'in ref') > > > struct Thing { } > Thing thing; > > const ref Thing func() { return gThing; } In this case the function "func" is declared const and not the return type. The reason for this is to allow this: class Foo { const: // All methods will be const } It's possible to do this with all attributes. > This syntax complains, but it's precisely the same expression I use to > pass an argument in to a function, and it's fine there: > remedy\modules\hud.d(35):Error: function remedy.hud.func without > 'this' cannot be const/immutable > > const ref Thing function() blah = &func; Again this declares the variable const and not the return type. I'm not sure about the "ref" thing. I don't know if function pointers can return by reference, at least not the syntax. > I need to take a pointer to this function, but It complains when I > declare a 'function' of this type: > remedy\modules\hud.d(36):Error: variable remedy.hud.blah only > parameters or foreach declarations can be ref (... what? it works for > parameters?) > > > I try rearranging the syntax to make the first issue stop complaining: > > ref const(Thing) func2() { return gThing; } // this seems to work now, > but i don't like the inconsistency... > ref const(Thing) function() blah2 = &func; This is the correct syntax to declare that it returns a const object. > Error: variable remedy.hud.blah2 only parameters or foreach declarations > can be ref Don't know about the reference. -- /Jacob Carlborg |
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| On 4 May 2012 12:52, Jacob Carlborg <doob@me.com> wrote:
> On 2012-05-04 10:38, Manu wrote:
>
>> This syntax complains, but it's precisely the same expression I use to
>
> pass an argument in to a function, and it's fine there:
>> remedy\modules\hud.d(35):**Error: function remedy.hud.func without
>> 'this' cannot be const/immutable
>>
>> const ref Thing function() blah = &func;
>>
>
> Again this declares the variable const and not the return type. I'm not sure about the "ref" thing. I don't know if function pointers can return by reference, at least not the syntax.
>
Ah, of course! I didn't spot that >_<
Thanks.
I suppose technically, 'ref' can lead to the same ambiguity. This must be the core of the problem. ref needs to be supported with parentheses?
|
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 2012-05-04 12:09, Manu wrote: > Ah, of course! I didn't spot that >_< > Thanks. > > I suppose technically, 'ref' can lead to the same ambiguity. This must > be the core of the problem. ref needs to be supported with parentheses? I'm not sure, since you can't declare a variable as "ref" I think the syntax should work. But the syntax with parentheses should probably work as well, to be consistent with "const". -- /Jacob Carlborg |
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 4 May 2012 at 11:49:44 UTC, Jacob Carlborg wrote:
> On 2012-05-04 12:09, Manu wrote:
>
>> Ah, of course! I didn't spot that >_<
>> Thanks.
>>
>> I suppose technically, 'ref' can lead to the same ambiguity. This must
>> be the core of the problem. ref needs to be supported with parentheses?
>
> I'm not sure, since you can't declare a variable as "ref" I think the syntax should work. But the syntax with parentheses should probably work as well, to be consistent with "const".
Const can be used with parentheses because it is also a type qualifier in addition to a storage class (for syntactic purposes). All of const, immutable and shared have this dual syntactic existence.
'ref' has several meanings depending on context, but it is never a type qualifier.
I think conflating the syntax for the two would mostly bring confusion.
|
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| On 4 May 2012 14:49, Jacob Carlborg <doob@me.com> wrote:
> On 2012-05-04 12:09, Manu wrote:
>
> Ah, of course! I didn't spot that >_<
>> Thanks.
>>
>> I suppose technically, 'ref' can lead to the same ambiguity. This must be the core of the problem. ref needs to be supported with parentheses?
>>
>
> I'm not sure, since you can't declare a variable as "ref" I think the syntax should work. But the syntax with parentheses should probably work as well, to be consistent with "const".
You can declare a variable as ref in the parameter list, that's where the ambiguity could arise, same with const.
|
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 05/04/2012 10:38 AM, Manu wrote: > So here's some problems. > > I use 'const ref' to pass structs to functions (note: because 'in ref' > doesn't seem to work) > And I often need to return structs by ref too, but I'm having problems: > > void test( const ref Thing x ) {} // this works fine. note, 'const ref' > works fine here (in lieu of 'in ref') > > > struct Thing { } > Thing thing; > > const ref Thing func() { return gThing; } > > This syntax complains, but it's precisely the same expression I use to > pass an argument in to a function, and it's fine there: > remedy\modules\hud.d(35):Error: function remedy.hud.func without > 'this' cannot be const/immutable > > const ref Thing function() blah = &func; > It is not the same, because it is parsed like const{ ref Thing func() { return gThing; } > I need to take a pointer to this function, but It complains when I > declare a 'function' of this type: > remedy\modules\hud.d(36):Error: variable remedy.hud.blah only > parameters or foreach declarations can be ref (... what? it works for > parameters?) > > > I try rearranging the syntax to make the first issue stop complaining: > > ref const(Thing) func2() { return gThing; } // this seems to work now, > but i don't like the inconsistency... > ref const(Thing) function() blah2 = &func; > > Error: variable remedy.hud.blah2 only parameters or foreach declarations > can be ref This is parsed like ref { const(Thing) function() blah2 = &func; } i.e. it qualifies the variable instead of the type. This should work: const(Thing) function()ref blah2 = &func; Except that it does not, because 'ref' is not currently a valid function 'storage class'. This seems to be an issue that deserves a bug report. |
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Friday, 4 May 2012 at 12:45:23 UTC, Timon Gehr wrote:
> This should work:
>
> const(Thing) function()ref blah2 = &func;
>
> Except that it does not, because 'ref' is not currently a valid function 'storage class'. This seems to be an issue that deserves a bug report.
For ref functions, 'ref' is part of the return type syntax, much like auto functions. There is no precedent for 'ref' working as a function attribute; it never was one.
|
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jakob Ovrum | On 05/04/2012 03:00 PM, Jakob Ovrum wrote: > On Friday, 4 May 2012 at 12:45:23 UTC, Timon Gehr wrote: >> This should work: >> >> const(Thing) function()ref blah2 = &func; >> >> Except that it does not, because 'ref' is not currently a valid >> function 'storage class'. This seems to be an issue that deserves a >> bug report. > > For ref functions, 'ref' is part of the return type syntax, If it was, then ref const(Thing) function() would work. > much like auto functions. > There is no precedent for 'ref' working as a function > attribute; it never was one. > It is an attribute: int x; ref { int foo(){ return x; } int bar(){ return x; } } ref: int qux(){ return x; } static assert(typeof(&qux).stringof == "int function() ref"); |
May 04, 2012 Re: Return by 'ref' problems... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Friday, 4 May 2012 at 14:34:20 UTC, Timon Gehr wrote:
> It is an attribute:
>
> int x;
> ref {
> int foo(){ return x; }
> int bar(){ return x; }
> }
>
> ref:
>
> int qux(){ return x; }
>
> static assert(typeof(&qux).stringof == "int function() ref");
Thanks, this is news to me! I never noticed that ref was actually a function attribute.
The following are legal declaration statements:
extern(C) void function() foo;
pure void function() bar; // not just linkage attributes
So I think it's a plain bug that this isn't:
ref void function() foo;
|
Copyright © 1999-2021 by the D Language Foundation