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
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?
@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 its 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 its 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 its 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 its 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 its 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 its 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 dont need to check the Variant.Type to see if its 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 its not NIL, but if its set to NIL, it can return an Integer, which is contradictory.
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
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.
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
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.
[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…