# Auto vs. Variant, when to use Auto?

**URL:** <https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169>\
**Category:** General\
**Created:** [May 11, 2015, 3:50am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169 "2015-05-11T03:50:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 3:50am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/1 "2015-05-11T03:50:54Z")

</div>

I’m having a hard time finding a good use for `Auto`. `Auto` uses less memory and is faster than a `Variant` if you know its type. If you do not know its type and have to determine that, `Auto` is much slower than `Variant`. You also can not use `IsA` on an `Auto` safely.

I did a simple benchmark, assigning a `String`, `Integer`, `Double` and `Date` to a `Variant` and `Auto` 10,000 times. In the respective loops, I assigned `Variant.Type` to an `Integer` (to simulate me checking what type it is) and for `Auto`, assigned `xojo.Introspection.GetType(var).Name` to a `String` value (to simulate me checking what type it is).

The `Variant` code ran in 0.0118245 seconds while the `Auto` code ran in 0.2940093 seconds.

BTW, `IsA` will work on `Auto` as long as you know it is an `Object`. However, if you are unsure, then you may cause Exceptions in your code by using `IsA` on an `Auto`. For example, the following code will cause an exception.

```auto
dim a as Auto = 10
Print Str(a isa Date)
```

So, when would one use an `Auto`? I know it uses less memory, but none of my apps are having memory issues right now. I use `Variant` rarely, but when I need to, the use cases for them will be much faster than an `Auto`.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 11, 2015, 3:59am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/2 "2015-05-11T03:59:19Z")

</div>

The second part (the potential of an exception with IsA) feels like a bug. Is it?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 4:20am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/3 "2015-05-11T04:20:14Z")

</div>

BTW, my benchmark code is simply:

```auto
  const its = 10000
  
  dim ms as Double
  dim s as String = "Hello"
  dim i as Integer = 10
  dim d as Double = 20.5
  dim dt as new Date
  
  dim vtype as Integer
  dim stype as String
  
  ms = Microseconds
  for j as Integer = 0 to its
    dim v as Variant
    
    v = s
    vtype = v.Type
    
    v = i
    vtype = v.Type
    
    v = d
    vtype = v.Type
    
    v = dt
    vtype = v.Type
  next
  Print Str(Microseconds - ms)

  ms = Microseconds
  for j as Integer = 0 to its
    dim a as Auto
    
    a = s
    stype = xojo.Introspection.GetType(a).Name
    
    a = i
    stype = xojo.Introspection.GetType(a).Name
    
    a = d
    stype = xojo.Introspection.GetType(a).Name
    
    a = dt
    stype = xojo.Introspection.GetType(a).Name
  next
  Print Str(Microseconds - ms)
```

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [May 11, 2015, 7:36am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/4 "2015-05-11T07:36:35Z")

</div>

To interrogate Auto is way slower than using a Variant (currently approx. 10 times). I trust that this will be getting better when the new framework develops.

But there are also quirks, like there is no GetTypeInfo for Xojo.Introspection, so you have to create an dummy object to get its TypeInfo. Or you cannot test if an Auto variable holding a Xojo.Core.Date is a Xojo.Core.Date with IsA. And there is more… Again I trust that this will get better over the time as the new framework is still young.

Auto is a great idea, but it needs a better API which currently is no API at all. I don’t understand why there is no Type property like with Variants. The issue with Variants was not that one could test what data type it was holding, but the implicit conversions which were built-in. So IMHO they should introduce this feature to Auto:

`Dim anAuto As Auto = ...
Select Case anAuto.Type
Case Auto.TypeInteger
  ...
Case Auto.TypeText
  ...
Case Auto.TypeObject
  ... 
  ...
End`

I also often test my application by looking at all the runtime objects in the debugger before quitting the application (to see if there are memory leaks). Just because of one Xojo.Core.Introspection call this is nearly impossible because introspection creates TypeInfos for the whole application and caches them. Last year I filed a feature request for this (35899 - Introspection: Ability to clear the cache).

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 11, 2015, 9:56am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/5 "2015-05-11T09:56:05Z")

</div>

Back in beta last year, Joe Ranieri explained that Introspection was providing the type. Implied was that a special property was not necessary. Maybe the benchmark Jeremy conducted will engage him to reconsider.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [May 11, 2015, 11:20am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/6 "2015-05-11T11:20:00Z")

</div>

I did that months ago. The introspection overhead is causing a lot of trouble.  
We do want to have select case mit integer types like variant for the speed. Especially I don’t want to have temp objects created and tons of string comparisons for each variant. Especially if you do something in a loop a 1000 times.

---

<div class="post-metadata">

**Author:** ![KarenA](https://forum.xojo.com/letter_avatar_proxy/v4/letter/k/c89c15/32.png) [@KarenA](https://forum.xojo.com/u/KarenA)\
**Post date:** [May 11, 2015, 11:57am UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/7 "2015-05-11T11:57:51Z")

</div>

Given the comparative slowness of using introspection to get Auto type, maybe Joe could elaborate on the reasons behind not providing a faster type property.

I wonder if part of it may be to discourage the use of Auto’s when not absolutely necessary?

- Karen

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [May 11, 2015, 12:07pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/8 "2015-05-11T12:07:18Z")

</div>

[quote=186269:@Karen Atkocius]Given the comparative slowness of using introspection to get Auto type, maybe Joe could elaborate on the reasons behind not providing a faster type property.

I wonder if part of it may be to discourage the use of Auto’s when not absolutely necessary?

- Karen[/quote]

Then discourage dictionary as well…

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [May 11, 2015, 12:49pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/9 "2015-05-11T12:49:42Z")

</div>

Because an integer property can’t tell you enough. With variant, should type return TypeDate or TypeObject for a Date? Both are true. Obviously, it should return TypeDate, but that implies it is not an object.

Arrays are a mess too. An integer just can’t give you the details effectively.

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [May 11, 2015, 1:07pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/10 "2015-05-11T13:07:36Z")

</div>

> [@186286:@Thom McGrath](#):
>
> Because an integer property can’t tell you enough

It does. This part of the Variant data type worked very well.

In the end what we all will do is have an Extends method on Auto mimicking Variant.Type in a very long  and slow  Select statement  while the compiler knows and could provide the type in an easy way.

I am very much in favor of the new framework and its increased type safety (and also the increase of encoding safety on Text compared to String). But not having Auto.Type is bad. The issues with Variants were the implicit conversions, not that you could interrogate the variable about its type.

The example for Date is the one exception, and it is mentioned in the docs and probably never posed a problem to anybody, as it shows in autocomplete.

> [@](#):
>
> Although Date is a class that is subclassed from Object, VarType identifies a Date as a Date data type (Type=7) rather than an Object (Type=9).

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [May 11, 2015, 1:13pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/11 "2015-05-11T13:13:02Z")

</div>

I agree with Eli. The Type property is not comprehensive, but Introspection and IsA are available when more detail is needed.

If Xojo were to implement Auto.Type and do away with TypeDate so a Date would simply read as Object, I’d be fine with that. I wouldn’t mind code that looked like:

```auto
if a.Type = Auto.TypeObject and ( a IsA Xojo.Core.Date or a IsA Global.Date ) then
```

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [May 11, 2015, 2:05pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/12 "2015-05-11T14:05:35Z")

</div>

Would it not be possible to write a class that wraps Auto and provides the type functionality of variant? Something like this (on mobile so sorry for any typos):

```auto
Class BetterAuto
Private value as Auto
Public Type as VarTypes

Public enum VarTypes
TypeInteger
TypeLong
TypeObject
//etc you get the idea

Function Operator_Convert() as Auto
Return value
End function

Sub Operator_Convert(inVal as Integer)
Value = inVal
Type = VarTypes.TypeInteger
End Sub

//more operator converts for each varType
```

Then you wouldn’t need introspection anymore so determining the type will be faster. Just an idea I had after reading this discussion. Maybe someone wants to make the class?

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 2:08pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/13 "2015-05-11T14:08:56Z")

</div>

> [@186306:@Jason King](#):
>
> Would it not be possible to write a class that wraps Auto and provides the type functionality of variant? Something like this (on mobile so sorry for any typos):

Sure, but you then have more memory overhead which is one of the benefits of `Auto` as I understand it and also more cumbersome to use with API expecting Auto’s or returning Autos.

Edit (Paul): Fixed quote tags

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [May 11, 2015, 2:20pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/14 "2015-05-11T14:20:07Z")

</div>

But then you can stay with Variants. I like the type safety which comes with Auto. I would never write an Extension method with Operator\_Convert on Auto as this re-introduces the problems we have all faced with Variants.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [May 11, 2015, 2:23pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/15 "2015-05-11T14:23:15Z")

</div>

[quote=186307:@Jeremy Cowgar]  
Sure, but you then have more memory overhead which is one of the benefits of `Auto` as I understand it and also more cumbersome to use with API expecting Auto’s or returning Autos.[/quote]  
Well I don’t see a huge memory overhead since you are only adding one property for the type which has a negligible impact on memory, especially in today’s computers. The beauty of operator convert is the class will automagically convert to Auto just by passing an instance of the class into a method which expects type auto, and will automagically convert from any other type (integer, Text, object, etc) when it is assigned to the variable. Of course there is still the case where the method returns an Auto, but you should be able to change those methods to return an explicit type, in which case it wouldn’t be a problem, or we could have an operator convert or constructor which accepts a variable of type auto and then uses the select case mentioned above to determine the type only in that case.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 2:23pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/16 "2015-05-11T14:23:19Z")

</div>

> [@186309:@Eli Ott](#):
>
> But then you can stay with Variants. I like the type safety which comes with Auto. I would never write an Extension method with Operator\_Convert on Auto as this re-introduces the problems we have all faced with Variants.

I don’t think his proposed class would. It doesn’t do any automatic conversion. It stores the value as is into an Auto, but while storing it, it is able to use the method type (via overloading) to set an additional .Type property. So, his class would still have the type safety but provide a .Type property to look at to see what type of Auto it contains.

The problem would be in actual use with the existing API and also the fact that you are now introducing classes to manage things which takes away from the memory efficiency of Auto.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 2:24pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/17 "2015-05-11T14:24:44Z")

</div>

> [@186311:@Jason King](#):
>
> Well I don’t see a huge memory overhead since you are only adding one property for the type which has a negligible impact on memory, especially in today’s computers. The beauty of operator convert is the class will automagically convert to Auto just by passing an instance of the class into a method which expects type auto, and will automagically convert from any other type (integer, Text, object, etc) when it is assigned to the variable. Of course there is still the case where the method returns an Auto, but you should be able to change those methods to return an explicit type, in which case it wouldn’t be a problem, or we could have an operator convert or constructor which accepts a variable of type auto and then uses the select case mentioned above to determine the type only in that case.

It’s certainly a move in the right direction, but the best solution would be a .Type property on the Auto itself from Xojo.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [May 11, 2015, 2:27pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/18 "2015-05-11T14:27:38Z")

</div>

> [@186314:@Jeremy Cowgar](#):
>
> It’s certainly a move in the right direction, but the best solution would be a .Type property on the Auto itself from Xojo.

I completely agree with you. But until we get that (if we get that) then we will have to make do with something like that class or the continued use of long and slow select case statements.

---

<div class="post-metadata">

**Author:** ![Jeremy\_Cowgar](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremy_cowgar/32/4590_2.png) [@Jeremy\_Cowgar](https://forum.xojo.com/u/Jeremy_Cowgar)\
**Post date:** [May 11, 2015, 2:38pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/19 "2015-05-11T14:38:11Z")

</div>

> [@186316:@Jason King](#):
>
> I completely agree with you. But until we get that (if we get that) then we will have to make do with something like that class or the continued use of long and slow select case statements.

I guess my question is, are we going to get better type determination with Auto?

---

<div class="post-metadata">

**Author:** ![Bob\_Keeney](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bob\_Keeney](https://forum.xojo.com/u/Bob_Keeney)\
**Post date:** [May 11, 2015, 3:32pm UTC](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169/20 "2015-05-11T15:32:41Z")

</div>

> [@186318:@Jeremy Cowgar](#):
>
> I guess my question is, are we going to get better type determination with Auto?

Only @Joe Ranieri can answer that. That’s assuming he’s allowed to answer because it might give away some long term plan. At XDC I heard a couple of comments that lead me to believe Auto is going to get way more useful in the future.

[Next page](https://forum.xojo.com/t/auto-vs-variant-when-to-use-auto/24169.md?page=2)
