Using out qualifier instead of *, how to pass null?
Derek Parnell
derek at nomail.afraid.org
Thu Feb 1 16:09:34 PST 2007
On Fri, 02 Feb 2007 00:38:30 +0100, xs0 wrote:
> Derek Parnell wrote:
>> On Thu, 01 Feb 2007 03:00:01 -0500, Rick Mann wrote:
>>
>>> In wrapping this C API, I've been going back and forth
>>> between converting "Foo*" output parameters to "out Foo".
>>> I prefer the latter, as I don't have to take the address
>>> at the call site, but I don't seem to be able to pass null
>>> (which the API allows, meaning, "I'm not interested in
>>> this return value").
>>>
>>> Am I just stuck with Foo*? Alternatively, I can overload
>>> the C function with D wrappers, I suppose. Hmm. That's
>>> probably best.
>>
>> Is not possible to use a 'null' object ... ?
>>
>> class Foo {}
>> void Bar(out Foo f)
>> {
>> if (f is null)
>> ...
>> else
>> ...
>> }
>> Foo NullFoo;
>> Foo RealFoo = new Foo;
>>
>> Bar(NullFoo);
>> Bar(RealFoo);
>
> hmm.. doesn't out cause (f is null) to be always true in that code?
LOL... yes it does. I was concentrating on the problem - "I don't seem to
be able to pass null", but missed that obvious gotcha. This code below
actually works though...
// ------------
import std.stdio;
class Foo
{
char[] m_id;
this(char[] x) { m_id = x.dup; }
}
void Bar(inout Foo f)
{
if (f is null)
writefln("NULL");
else
writefln("Got %s", f.m_id);
}
void main()
{
Foo NullFoo;
Foo FooA = new Foo("Fred");
Foo FooB = new Foo("Betty");
Bar(NullFoo);
Bar(FooB);
Bar(FooA);
}
// ------------
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
2/02/2007 11:04:56 AM
More information about the Digitalmars-d-learn
mailing list