Recent

Author Topic: SomeVariant.Vtype. Why?  (Read 417 times)

jamie

  • Hero Member
  • *****
  • Posts: 1899
SomeVariant.Vtype. Why?
« on: September 03, 2019, 09:33:07 pm »
The Compiler allows me to access a Variant.Vtype at compile time with no issues, however, at runtime I get an error?

If SomeVariant.Vtype = 0 Then Beep;

Instead I have to cast it.

If TVarData(V).VType = 0...…

Why do I need to do this if the compiler knows about the field at compile time?

P.S.
 This also happens in my OLD Delphi 3! Do we really have to copy everything from Delphi?

ASerge

  • Hero Member
  • *****
  • Posts: 1392
Re: SomeVariant.Vtype. Why?
« Reply #1 on: September 04, 2019, 05:18:08 pm »
The Compiler allows me to access a Variant.Vtype at compile time with no issues, however, at runtime I get an error?
If SomeVariant.Vtype = 0 Then Beep;
Instead I have to cast it.
If TVarData(V).VType = 0...…
Why do I need to do this if the compiler knows about the field at compile time?
It doesn't know. For compatibility with Delphi, the dispatching interface is supported and any valid identifiers are allowed. I. e. at compile time this code will allowed
Code: Pascal  [Select]
  1. V.AnyValidIdentifier
At run time, of course, there will be an attempt to call the dispatch interface, and through it the specified method.

jamie

  • Hero Member
  • *****
  • Posts: 1899
Re: SomeVariant.Vtype. Why?
« Reply #2 on: September 04, 2019, 05:32:57 pm »
Hmm, interesting..

 So the compiler is treating the variant as a interface call, I thought that only for the OleVariant?

 Shouldn't the compiler flag a warning here?

ASerge

  • Hero Member
  • *****
  • Posts: 1392
Re: SomeVariant.Vtype. Why?
« Reply #3 on: September 04, 2019, 07:40:12 pm »
Shouldn't the compiler flag a warning here?
No. Variant type was invented for such purposes. If you need more strict validation, try not to use variables of this type.

jamie

  • Hero Member
  • *****
  • Posts: 1899
Re: SomeVariant.Vtype. Why?
« Reply #4 on: September 04, 2019, 08:36:50 pm »
If you say so, I was just experimenting and trying to reduce clutter in a couple of automation apps where I use Variants extensively, basically HMI interface while creating a configuration works a lot like the PHP language does where as a variable can be just about anything.

 If I were to create my own variant type record I would end up being in same position but would take time to do that. So why recreate the something that is already been debugged for years.

PascalDragon

  • Hero Member
  • *****
  • Posts: 564
  • Compiler Developer
Re: SomeVariant.Vtype. Why?
« Reply #5 on: September 09, 2019, 10:10:20 am »
So the compiler is treating the variant as a interface call, I thought that only for the OleVariant?
The difference between Variant and OleVariant is that the latter only allows types that are supported by Microsoft's OLE while the former also supports Object Pascal specific types (though not necessarily all).

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 651
    • Lebeau Software
Re: SomeVariant.Vtype. Why?
« Reply #6 on: September 10, 2019, 10:46:29 pm »
while [Variant] also supports Object Pascal specific types

And user-defined types, too (via TCustomVariantType).
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 8673
Re: SomeVariant.Vtype. Why?
« Reply #7 on: September 11, 2019, 08:19:02 am »
But still the crux is the compiler resolves any variant type at run-time, not compile time..... Even if it can somehow know how it can be resolved into a valid compile time type.
(It should not even attempt to do that)
The theory is well described here: https://en.wikipedia.org/wiki/Tagged_union
Demo - this compiles!! as it should:
Code: Pascal  [Select]
  1. {$mode delphi}{$H+}
  2. var
  3. a,b:Variant;
  4. begin
  5.   a:= 0; // valid
  6.   b:='Test'; // valid
  7.   writeln(a+b); // run-time error, I hope, guess what? I does!!! Happy...
  8. end.

Ergo, don't use variants unless you know what you are doing.
Worse: if you actually know it can be resolved at compile time you are a complete moron to use a variant type. Use the proper type.

This is not even open for discussion.... IMNSHO
We are not copying from Delphi, alas, as the theory is implemented as it should be.

I have the faint idea you will refuse to understand this anyway.
Sometimes you have to put in some more effort in trying to understand things.
« Last Edit: September 11, 2019, 08:57:10 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

jamie

  • Hero Member
  • *****
  • Posts: 1899
Re: SomeVariant.Vtype. Why?
« Reply #8 on: September 11, 2019, 02:00:44 pm »
That's fine, I understand how it works behind the scenes.

The last thing I want is this @Thaddy
https://en.wikipedia.org/wiki/Software_bloat
https://www.youtube.com/watch?v=NsiVDeFlTJI

Have a good day  :)