Variant = NIL, bug?

I have a Variant which is set to NIL by purpose, to distinguish when it contains something.
Now doing this:

dim v as Variant = nil dim i as Integer = v.IntegerValue

I get no error and “i” is set to 0

I believe trying to get the IntegerValue from a NIL Variant I should get a NilObjectException, but this is not what happen.
And for me this is a bug.

Any thoughts before I open a new report?

I would expect that only if you set “V” to an object or class instance… I would expect all “standard” datatypes to be coerced to their default value as “integer” cannot be null/nil

I don’t think this is a bug, just an edge case you are not aware of.

If you did:

dim v as variant = nil   ' which doesn't really make sense as just saying dim v as variant results in a nil value

dim s as string = v.stringvalue

s would equal a null string: “”

So I see nothing wrong with this. Since the variant is not an object and the variant is not containing an object, how can you have a Nil OBJECT exception?

[quote=129717:@Massimo Valle]I have a Variant which is set to NIL by purpose, to distinguish when it contains something.
Now doing this:

dim v as Variant = nil dim i as Integer = v.IntegerValue

I get no error and “i” is set to 0

I believe trying to get the IntegerValue from a NIL Variant I should get a NilObjectException, but this is not what happen.
And for me this is a bug.

Any thoughts before I open a new report?[/quote]

Dixit here http://en.wikipedia.org/wiki/Nil
Nil is a word commonly used to mean nothing or zero; it is one of several names for the number 0.

If Xojo is any coherent with the mathematical notion, the result is correct.

Notice that v.type=0 (typeNil) after assigning nil. So, nil is actually a valid value (type?) for a variant to hold.

@Jon Ogden
I explicitly set it to NIL to make explicit in the code what I want, so it makes sense as it make sense to do (x * y) + z

@Michel Bujardet
NIL and 0 are not the same thing. It is true that in computer languages, a pointer to an invalid object is set to 0, but it’s not the same thing.

In fact the main purpose of having a property set to NIL is exactly to distinguish it from 0. So if the Variant can be set to NIL (as @jim mckay pointed) I expect it makes some difference from 0 when it’s supposed to contain an integer.

This is easily proven with the following code:

dim v as variant = 0 if v = nil then msgBox "I'm a donkey" end if

The message box will never show, and this is correct.
But following what I read above, it should say it’s NIL, because it contains no object.

[quote=129733:@Massimo Valle]@Jon Ogden
I explicitly set it to NIL to make explicit in the code what I want, so it makes sense as it make sense to do (x * y) + z

@Michel Bujardet
NIL and 0 are not the same thing. It is true that in computer languages, a pointer to an invalid object is set to 0, but it’s not the same thing.

In fact the main purpose of having a property set to NIL is exactly to distinguish it from 0. So if the Variant can be set to NIL (as @jim mckay pointed) I expect it makes some difference from 0 when it’s supposed to contain an integer.

This is easily proven with the following code:

dim v as variant = 0 if v = nil then msgBox "I'm a donkey" end if

The message box will never show, and this is correct.
But following what I read above, it should say it’s NIL, because it contains no object.[/quote]

Jim has got a way to distinguish between v = nil and v = 0. In the first case, v.type = 0, and the second v.type = 2.

Michel, you don’t need to check the Variant.Type to see if it’s NIL. You check it as I shown above.
But my point was exactly to show that Variant makes a distinction from NIL to 0, even when it contains a compound data type like integer.

To resume, if a Variant contains an integer, then it’s not NIL, but if it’s set to NIL, it can return an Integer, which is contradictory.

Why not read the fabulous manual or is that the fantastic manual?

I created a series of classes that serve this function. They each emulate one of the intrinsic classes but, as objects, can be set to nil. I designed them to be used in params so you can tell when something was actually set.

i haven’t looked at them in a while, but I’ll make them available if that will help.

Thanks Kem, I was about to write something like that, so I will first wait for your :stuck_out_tongue:

In any case, I still feel the Variant behavior is wrong because it’s not consistent.
It should return always NIL if it doesn’t contains an object or it should never return NIL.
Now it returns NIL only if nothing was assigned to it or it was explicitly set to NIL.

[quote=129717:@Massimo Valle]I have a Variant which is set to NIL by purpose, to distinguish when it contains something.
Now doing this:

dim v as Variant = nil dim i as Integer = v.IntegerValue

I get no error and “i” is set to 0

I believe trying to get the IntegerValue from a NIL Variant I should get a NilObjectException, but this is not what happen.
And for me this is a bug.

Any thoughts before I open a new report?[/quote]

It’s not a bug, just part of how Variant is special.

But the point if a variant is that it can contain ANYTHING.
The language casts it to whatever you want to use it as on-the-fly

So if you do

v = nil
dim f as folderitem  
f = v

I would expect f to be nil because it is an object and objects can be nil

But if you do

dim x as integer x = nil

what would you expect to happen?

Xojo looks at the target.
Is it a string?
You will get the best shot at a string … i.e. “”
Is it a number?
You will get the best shot at converting to a number i.e. 0

Is it an object?
You’ll get nil

Looks fine to me.

Here you go.

https://dl.dropboxusercontent.com/u/26920684/Param%20Classes.zip

These are really meant as params only so you wouldn’t use them directly in calculations, for example. They are also a stopgap measure until Xojo introduces them natively.

As others have stated, a variant cannot BE nil, it can CONTAIN a nil. Use Variant.IsNull to test for whether the variant contains a nil value.

[quote=129717:@Massimo Valle]I have a Variant which is set to NIL by purpose, to distinguish when it contains something.
Now doing this:

dim v as Variant = nil dim i as Integer = v.IntegerValue

I get no error and “i” is set to 0

I believe trying to get the IntegerValue from a NIL Variant I should get a NilObjectException, but this is not what happen.
And for me this is a bug.

Any thoughts before I open a new report?[/quote]

Don’t bother
Its by design - see Joe’s reply
And this behavior is documented in the LR under Variant as Syed points out

Nothing as this won’t compile at all :slight_smile:

[quote=129760:@Massimo Valle]In any case, I still feel the Variant behavior is wrong because it’s not consistent.
It should return always NIL if it doesn’t contains an object or it should never return NIL.
Now it returns NIL only if nothing was assigned to it or it was explicitly set to NIL.[/quote]

I think the variant behavior is designed to make the most reasonable conversion between types… (right?)

assign a ptr and use integerValue = address of the ptr
assign a date and use doubleValue = totalseconds
assign a string (“OK”) and use integerValue = 0 (can’t reasonably convert that string to a number)
assign a string (“15”) and use integerValue = 15 (reasonable conversion)
assign a nil value and use integerValue = 0 (can’t reasonably convert nil to a number)

That makes me wonder though if doubleValue should be NaN if no reasonable conversion can be made (ie “XYZ”=NaN Nil=NaN etc…)
Currently it returns 0, same as integerValue. Could break a lot of code though…

Variant is broken. This does not work :

Dim v as variant = Kitchen Sink

[quote=129818:@Michel Bujardet]Variant is broken. This does not work :

Dim v as variant = Kitchen Sink

of course not - syntax error :stuck_out_tongue: