I have:
var myvar, yorvar as new myclass
Then I can do:
myvar = Nil
but if I do:
myvar = if (someCondition, yorvar, Nil)
then I get a compile error. The message is (approximately):
Expected value of type myClass but got Nil
Is there any valid reason for this? Workaround is easy, of course.
heh … remove the NEW on
var myvar, yorvar as new myclass
oddly enough in 2019r1.1 I get a very different compilation error
A Dim statement creates only one new object at a time
Dim myvar , yorvar As new Class1
Sorry, I should have said in my OP, If I have:
var myvar as myclass
Then doing this:
myvar = if (someCondtion, someValue, Nil)
gives my error message regarding Nil.
what type is somevalue ?
if() is funny and it assumes the return types are consistent so if someValue is not compatible with nil then you’ll get this error
for example this gives that kind of error
Dim myvar As Class1
Dim yorvar As New Class1
myvar = If (True, 123, Nil)
OK - it’s a value from a dictionary. The value previously put into the dictionary was of type myclass, but I suppose the value returned is a variant, strictly speaking.
Can I cast the Nil? Thus:
myvar = if (someCondition, myClass(someValue), myClass(Nil))
Edit: Humph. Well, that generated no errors.
You can cast NIL but there’s no real reason to
NIL is compatible with any instance reference type (and a few others)
So the cast on the value from the dictionary should be all you need
FWIW this is where I would LOVE to have a “TypedDictionary” where you can do something like
var myDict as TypedDictionary<string, myClass>
to indicate your dictionary has string keys and ALL values will be of type myClass
And then when you get a value out it WILL be a myClass - not a variant
Same for other types so you can actually get the compiler to do more validation of your code when you use things like dictionaries, pairs, or anything else that today uses variants to represent ANY type
But I doubt we’ll see generic or templated type any time soon
1 Like