Why is Introspection.GetType Nil?

OK. This is weird.

I’m contributing to Express (the API 2.0 fork of Aloe Express) and I think I have come across a bug in the Xojo framework.

Check out this screenshot of the debugger and the related code:

As you can see from the code, value is a Variant that contains a value. valueType seems to return Nil when value is not Nil. It doesn’t matter if value is a String or a Boolean, valueType still returns Nil. valueType is sometimes not Nil (for example if value is a JSONItem).

Am I (and the original author of the code) misunderstanding Introspection.GetType here?

In fact I can simplify this:

Var value As Variant = False
Var valueType As Introspection.TypeInfo = Introspection.GetType(value)
Break // valueType is Nil. Why?
1 Like

You cannot get TypeInfo on an intrinsic type, only objects, and your value here contains a string.

Instead use the Variant.Type property to determine the contents of your Variant.

(The Xojo.Introspection version did allow this, and there is a request to add it to the standard Introspection.)

On the plus side, using the Type property is much faster.

Ah I see.

What if value could be an intrinsic type or a class. How should I best elegantly handle that scenario? Should I do a Variant.Type check for a primitive and then fall through to TypeInfo?

I don’t think the docs are clear on this.

It depends on how much information you need, but maybe something like this:

select case value.Type
case Variant.TypeString
  // ...
case Variant.TypeBoolean
  // ...
case Variant.TypeDate
  var d as Date = value
  // ...
case Variant.TypeDateTime
  var dt as DateTime = value
  // ...
case Variant.TypeObject
  var ti as Introspection.TypeInfo = Introspection.GetType( value )
  // ...

2 Likes
If myVariant IsA Object Then
// This is an object?
end if
2 Likes

Perfect. I think that’s the best solution.

I’ll try and find the feedback case and add a thumbs up.

here it is:

https://tracker.xojo.com/xojoinc/xojo/-/issues/62926

please subscribe/comment there.

1 Like

#62926 - Introspection.GetType should work on intrinsic values

:wink:

2 Likes

This is awesome Tim. Thank you.

1 Like