self = nil?

In a constructor method for a class can I use self = nil when the constructor determines not all the data is available (for whatever reason) to create the object? Basically I want the constructor to return nil instead of an instantiated object.

No, but you could instead try raising an exception for this situation.

Fair call :slight_smile:

Nice game out there :slight_smile:

Sometimes I felt it useful being able to return a nil instance, but unfortunately this is not possible. Recursing to exceptions is the correct way I guess, but still not very elegant compared to just check if the returned instance is NIL.

How about creating a shared method for the class (e.g. createNewClassInstance) which returns a class instance, and making the constructor Private. This way, you can only create the class instance through the shared method, which you can determine if the data is correct, and return nil if not.

There is an example of a Factory method (what Mark describes) included with the Xojo examples (look in the Design Patterns folder).

That’s exactly what I do in this case. Still not the best.

I’m having tons of constructors where I need to return a Nil in a Constructor? What do you use for these cases?!?!

Thx!

Unlike C++ constructors are not ALLOCATORS - they are more like Java’s constructors which are initializers.
The memory has already been allocated by the time the constructor is called - so its impossible for you to throw that away.
Constructors are INITIALIZERS - basically something that is called by the framework to set up the object the way you want whenever NEW is called.

But you can’t get rid of the object
Hence why the signature for Constructor is NOT
Constructor() as TYPE
Constructors dont return anything

IF you need to return NIL to signal some kind of error :

  1. throw an exception
  2. use a factory

Thx @Norman Palardy . Both alternatives are great.

Anyone coming to Xojo from a “C++ like” language might expect to be able to “return nil”

I came from Java and this is almost identical to how Java behaves.
Except in Java you cannot call the constructor any old time like you can in Xojo.
In Xojo its just a method as far as Xojo is concerned. Nothing particularly special about it.

Call it when you want

The other thing to watch for is things like Operator_Convert also behaves like an initializer but does NOT call Constructor
So if you have a “convert from” operator_convert you may want to make sure you all Constructor in there as well so your objects are constructed consistently

“Convert from” takes on the form operator_convert(name as type)
http://documentation.xojo.com/index.php/Operator_Convert

Just a question about a kind of a ConstructorNilException

How would you handle this scenario were you have to instantiate a class and the constructor sends a ConstructorNilException via exceptions?

Will really appreciate this!

Thx

Not sure what you mean ?

if you were going to raise an exception you might do

[code]dim myFooInstance as FooClass

try
myFooInstance = new FooClass( arguments )
catch exce as ConstructorNilException
// whatever you need to do to deal with the fact you could not construct the object correctly
end try
[/code]

You don’t really even need to create a new exception type if you don’t want
Just raise a new nil object exception with suitable error messages … or not :slight_smile:

Norman, that the easy way ;). Imagine with some dozens of classes working as that.

I asked for a more… integrated way… if it’s something possible. :slight_smile:

I have no idea what you’re asking for ?

[quote=168153:@Amando Blasco]Norman, that the easy way ;). Imagine with some dozens of classes working as that.

I asked for a more… integrated way… if it’s something possible. :)[/quote]

Well, the correct way should be to raise an exception, the only drawback is you have to try…catch every creation of a new object.
As an alternative, to mimic a behavior where you get NIL, you can define a boolean flag in your class like ValidInstance As Boolean and set it to true or false on constructor.
Then you can make:

dim anObject as New MyObject if not anObject.ValidInstance then anObject = nil end if

or make a factory as Norman said.

this was a good question and thank you Paul and Norman I’ve learned a bit more today

[quote=168191:@Massimo Valle]Well, the correct way should be to raise an exception, the only drawback is you have to try…catch every creation of a new object.
As an alternative, to mimic a behavior where you get NIL, you can define a boolean flag in your class like ValidInstance As Boolean and set it to true or false on constructor.
Then you can make:

dim anObject as New MyObject if not anObject.ValidInstance then anObject = nil end if

or make a factory as Norman said.[/quote]

Great advice. Thank you all!

@Tomas Jakobs
Yeah, this is has been a great post!