Return by 'ref' problems...

Timon Gehr timon.gehr at gmx.ch
Fri May 4 05:45:22 PDT 2012


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.


More information about the Digitalmars-d mailing list