digitalmars.D.learn - Changing by ref a class passed in function
- Zaher Dirkey (23/23) Jan 22 2015 See example bellow, i want to pass object to function nullIt and
- anonymous (10/33) Jan 22 2015 o needs to be typed as Object:
- anonymous (5/8) Jan 22 2015 Alternatively, templatize nullIt:
- Zaher Dirkey (2/11) Jan 22 2015 Template way is better here, thank you.
See example bellow, i want to pass object to function nullIt and
want this function to null it.
----
import std.stdio;
static if (!is(typeof(writeln)))
alias writefln writeln;
class MyClass{
}
void nullIt(ref Object o)
{
o = null;
}
void main()
{
auto o = new MyClass();
nullIt(o);
if (o is null)
writeln("It is null");
}
Thanks in advance.
Jan 22 2015
On Thursday, 22 January 2015 at 13:06:42 UTC, Zaher Dirkey wrote:
See example bellow, i want to pass object to function nullIt
and want this function to null it.
----
import std.stdio;
static if (!is(typeof(writeln)))
alias writefln writeln;
class MyClass{
}
void nullIt(ref Object o)
{
o = null;
}
void main()
{
auto o = new MyClass();
nullIt(o);
if (o is null)
writeln("It is null");
}
Thanks in advance.
o needs to be typed as Object:
Object o = new MyClass();
nullIt(o);
If the compiler accepted your original code, it would be possible
to assign a non-MyClass to o:
void messWithIt(ref Object) o) {o = new Object;}
auto o = new MyClass();
messWithIt(o); /* If this compiled, o would be typed as
MyClass, but would not actually be a MyClass. */
Jan 22 2015
On Thursday, 22 January 2015 at 14:29:59 UTC, anonymous wrote:
o needs to be typed as Object:
Object o = new MyClass();
nullIt(o);
Alternatively, templatize nullIt:
void nullIt(O)(ref O o) {o = null;}
auto o = new MyClass();
nullIt(o);
Jan 22 2015
On Thursday, 22 January 2015 at 14:39:45 UTC, anonymous wrote:On Thursday, 22 January 2015 at 14:29:59 UTC, anonymous wrote:Template way is better here, thank you.o needs to be typed as Object: Object o = new MyClass(); nullIt(o);Alternatively, templatize nullIt: void nullIt(O)(ref O o) {o = null;} auto o = new MyClass(); nullIt(o);
Jan 22 2015








"Zaher Dirkey" <zaherdirkey yahoo.com>