Recent

Author Topic: Variant issue  (Read 479 times)

egsuh

  • Full Member
  • ***
  • Posts: 241
Variant issue
« on: August 04, 2019, 03:09:37 am »
I have written this issue quite a while ago, but don't you have any plan to fix this?

In this following example,

Code: Pascal  [Select]
  1. var
  2.     c: variant;
  3.     d, e : string;
  4.     i : integer;
  5. begin
  6.      c := 12;
  7.      d := 'some text';
  8.      e := 'another text';
  9.  
  10.       i := c;
  11.  
  12.       memo1.lines.add ('%d=%s|%s', [i, d, e]);    // This works
  13.       memo1.lines.add ('%d=%s|%s', [c, d, e]);   // but this does not.
  14. end;

The first add method works, but the second one does not. I think this should work.

ASerge

  • Hero Member
  • *****
  • Posts: 1411
Re: Variant issue
« Reply #1 on: August 04, 2019, 08:33:11 am »
Code: Pascal  [Select]
  1. var
  2.     c: variant;
  3. ...
  4. begin
  5. ...
  6.     memo1.lines.add ('%d=%s|%s', [c, d, e]);   // but this does not.
  7. end;
I think this should work.
No. For Variant types, only a string modifier is allowed. It's not documented, but is compatible with Delphi.

furious programming

  • Sr. Member
  • ****
  • Posts: 354
  • I click a little.
Re: Variant issue
« Reply #2 on: August 04, 2019, 11:36:27 am »
This is poor support, not compatibility. The type that allows to store data of multiple types, which can not be converted/formatted regardless of the content is a nonsense. Adding support for such a conversion of variants will not make backward/Delphi compatibility broken.
Lazarus 2.0.4 with FPC 3.0.4, Windows XP (all 32-bit)

ASerge

  • Hero Member
  • *****
  • Posts: 1411
Re: Variant issue
« Reply #3 on: August 04, 2019, 12:36:20 pm »
Adding support for such a conversion of variants will not make backward/Delphi compatibility broken.
That's possible. Make a bug, the implementation does not have to be complicated, moreover, in the JCL that has already been done. Another thing is that almost few people use Variants in the Format function.

Thaddy

  • Hero Member
  • *****
  • Posts: 9167
Re: Variant issue
« Reply #4 on: August 04, 2019, 01:29:28 pm »
We can' t add a type helper for example.
StrToInt(VarToStr(c)) is the only option I see. Of course there must be more.
I would expect something like this would be provided as standard?
Code: Pascal  [Select]
  1. function VarToInt(const v:Variant):integer;
  2. begin
  3.   if v.varType = varInteger then
  4.     Result := v
  5.   else
  6.     EVariantError.Create('variant is not an integer');
  7. end;
« Last Edit: August 04, 2019, 02:11:57 pm by Thaddy »
also related to equus asinus.

jamie

  • Hero Member
  • *****
  • Posts: 2073
Re: Variant issue
« Reply #5 on: August 04, 2019, 05:20:01 pm »
Well, its a question of do you need working code or are you just poking around?

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  var
  3.     c: variant;
  4.     d, e : string;
  5.     i : integer;
  6. begin
  7.      c := 12;
  8.      d := 'some text';
  9.      e := 'another text';
  10.       i := c;
  11.  
  12.       memo1.lines.add ('%d=%s|%s', [i, d, e]);    // This works
  13.       memo1.lines.add ('%d=%s|%s', [Integer(c), d, e]);   // This now works.
  14. end;                                                    
  15.  

Number 1 at blue screen app creations!

furious programming

  • Sr. Member
  • ****
  • Posts: 354
  • I click a little.
Re: Variant issue
« Reply #6 on: August 04, 2019, 05:32:06 pm »
Another thing is that almost few people use Variants in the Format function.

In that case, we have few pissed off users. I know that better few than few thousand, but it should work.
Lazarus 2.0.4 with FPC 3.0.4, Windows XP (all 32-bit)

jamie

  • Hero Member
  • *****
  • Posts: 2073
Re: Variant issue
« Reply #7 on: August 04, 2019, 05:46:33 pm »
I use variants a lot in two apps I have.They are a very important part of my development.

I have a automation app (HMI editor and screen viewer) that uses those types through out
for the property fields because I can have one entity and set it to what ever type it needs to be.

 SO the database for the fields can be any type instead of managing different types that require different functions etc.

 I did add some helpers to the list for the variants so that when I add a Integer + String variant the LEFT (integer) becomes the predominant operator and performs a StrToInt in that case and so on.
 
 Its like fluid as it should be, it just flows together.\

Number 1 at blue screen app creations!

Thaddy

  • Hero Member
  • *****
  • Posts: 9167
Re: Variant issue
« Reply #8 on: August 04, 2019, 05:55:13 pm »
Well, as long as you know when to use variants there is little against it, but do not misuse them because they are extremely slow, not 50% not 100% but magnitudes slower than native types..
also related to equus asinus.

Thaddy

  • Hero Member
  • *****
  • Posts: 9167
Re: Variant issue
« Reply #9 on: August 04, 2019, 06:18:52 pm »
In that case, we have few pissed off users. I know that better few than few thousand, but it should work.
As far as I know it also does not work in Delphi, not even 10.3.
also related to equus asinus.