escaping addresses of ref parameters - not

Daniel Keep daniel.keep.lists at gmail.com
Mon Feb 9 06:09:57 PST 2009



Denis Koroskin wrote:
> Christopher Wright Wrote:
> 
>> Daniel Keep wrote:
>>> I've used ref arguments in the past to wrap a C api that expects
>>> pointers.  I'm fine with this so long as there is a way to break out of
>>> it (in regular D, at least) that makes it abundantly clear you need to
>>> know what you're doing.
>>>
>>> Something like:
>>>
>>> void wrapSomeCApi(ref Foo arg)
>>> {
>>>     Foo* argptr = ref_unsafe_escape(arg);
>>>     some_c_api(argptr);
>>> }
>>>
>>> Incidentally, I don't suppose we can get ref variables while Walter's at
>>> it? :P
>>>
>>>   -- Daniel
>> Why aren't you passing a Foo*?
> 
> That's ok if Foo is a struct:
> 
> struct Rect
> {
>     int x, y, width, weight;
> };
> 
> class Widget {
>   // returns true on success
>   bool getBounds(ref Rect rect) {
>     Rect* rectPtr = ref_unsafe_escape(rect);
>     return gtk_widget_get_bounds(rectPtr) != 0;
>   }
> }
> 
> Another question is, what is the benefit of it? Why not take an adress directly?
> 
> return gtk_widget_get_bounds(&rect) != 0;

Because I treat pointers as "dragons be here" territory, and try to
restrict them to as little of my code as humanly possible.  I also feel
that if a function call is using pointers as an implementation detail, I
shouldn't have to specify it myself.

  -- Daniel



More information about the Digitalmars-d mailing list