Xojo.Core.WeakRef and Nil Values

Hey Gang,

Question on the new Xojo.Core.WeakRef class…

When creating a WeakRef you do something like this:

MyWeakRef = New WeakRef(Value)

In the old framework if value was NIL no errors were thrown.

In the new framework, this is done differently:

MyWeakRef = xojo.Core.WeakRef.Create(value)

If value is Nil, an exception gets thrown.

Is this expected behavior? Or should xojo.Core.WeakRef.Create be able to accept a nil object as a parameter?

I would think expected behavior. The philosophy is to throw an exception for anything ‘bad’. Fail early and fail loudly is the new mantra.

I don’t use WeakRef’s very often but I think trying to get a WeakRef to a nil value doesn’t make any sense. Check for nil before you attempt to get the WeakRef.

Obviously, @Joe Ranieri is the authority on this so maybe he’ll chime in.

I’m starting to use WeakRefs quite often when dealing with objects passed between different windows other objects. It’s really helped to alleviate issues related to an object still existing because the reference to it was not properly destroyed.

Anyhow, the issue isn’t so much in “getting” the WeakRef but in setting it. I use the suggested way of a computed property to store the WeakRefs. And sometimes, you will pass a nil value into the Getter of the computed property. Personally, I would think that the Create method should ignore a nil object as the parameter. It would save me from typing an If statement… :slight_smile: But it’s no big deal. I just wanted to make sure that throwing the exception was the intended result and not a bug. If it’s the way it’s supposed to be then it is different from the old framework and you just need to check that your object isn’t nil before attempting to create the WeakRef.

[quote=162879:@Bob Keeney]I would think expected behavior. The philosophy is to throw an exception for anything ‘bad’. Fail early and fail loudly is the new mantra.

I don’t use WeakRef’s very often but I think trying to get a WeakRef to a nil value doesn’t make any sense. Check for nil before you attempt to get the WeakRef.

Obviously, @Joe Ranieri is the authority on this so maybe he’ll chime in.[/quote]

This is entirely correct.