Recent

Author Topic: [SOLVED] Help with TVector (unit gvector)  (Read 2229 times)

Hi im Pascal

  • New Member
  • *
  • Posts: 40
[SOLVED] Help with TVector (unit gvector)
« on: June 15, 2021, 10:34:50 pm »
Hi all,

I'm a little sad, and maybe a little shocked...

howcome this compiles (and doesn't do what it looks to do):
Code: [Select]
purchases[i].Count -= numCovered;

and this doesn't (and why not?)
Code: [Select]
purchases[i].Count := purchases[i].Count - numCovered;

// Error: Argument cannot be assigned to

I also had tried:
Code: [Select]
purchases[i]^.Count -= numCovered;

// or even defining temp : ^TStockTransaction
temp := purchases[i];
temp^.Count -= numCovered;

But I cannot figure out how to make FPC use this access method (gvector source):
Code: [Select]
property Mutable[i : SizeUInt]: PT read getMutable;

consider purchases as TVector<TStockTransaction> and TStockTransaction is a simple record with some fields like TDateTime, String, Real, ...

Code: [Select]
type
TStockTransaction = record
    StockID : String;
    Basis : Real;
    Count : Real;
    Date : TDateTime;
  end;

type
  TTransactionVector = specialize TVector<TStockTransaction>;

There are many nice things about FPC/Lazarus, but some things really frustrate me, coming from C++ (of course, some things in C++ are also very frustrating coming from FPC/Lazarus, to be fair...)

>>> How can I get mutable access to records in a TVector? <<<

In fact, if you can point me to any good/best practice samples for TVector doing various tasks I'd appreciate that.

Thanks!
« Last Edit: June 15, 2021, 10:55:08 pm by Hi im Pascal »

zamtmn

  • Hero Member
  • *****
  • Posts: 594
Re: Help with TVector (unit gvector)
« Reply #1 on: June 15, 2021, 10:50:24 pm »
Code: Pascal  [Select][+][-]
  1. purchases.mutable[i]^.Count -= numCovered;

Hi im Pascal

  • New Member
  • *
  • Posts: 40
Re: Help with TVector (unit gvector)
« Reply #2 on: June 15, 2021, 10:50:50 pm »
OMG I'm so stupid, it's really THAT simple:
Code: [Select]
purchases.Mutable[i]^.Count -= numCovered;

I was thinking that the []operator magic somehow works like in C++

But then howcome does this work the same:
Code: [Select]
ShowMessage(FloatToStr(purchases[i].Count));
ShowMessage(FloatToStr(purchases.Items[i].Count));

I cannot find where in gvector the implicit []-subscript is defined, and I just realized there must be something weird going on between TVector and TVectorEnumerator... but I'll try to figure it out  %)

GOT IT
(Items is the default property):
Quote
The definition of an array property can be followed by the default directive, in which case the array property becomes the default property of the class. For example: ...
If a class has a default property, you can access that property with the abbreviation object[index], which is equivalent to object.property[index]. For example, given the declaration above, StringArray.Strings[7] can be abbreviated to StringArray[7]. A class can have only one default property with a given signature (array parameter list), but it is possible to overload the default property. Changing or hiding the default property in descendent classes may lead to unexpected behavior, since the compiler always binds to properties statically.
http://docwiki.appmethod.com/appmethod/1.13/topics/en/Properties

Great site...
« Last Edit: June 15, 2021, 11:03:43 pm by Hi im Pascal »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [SOLVED] Help with TVector (unit gvector)
« Reply #3 on: June 16, 2021, 07:48:15 am »
I'm a little sad, and maybe a little shocked...

howcome this compiles (and doesn't do what it looks to do):
Code: [Select]
purchases[i].Count -= numCovered;

and this doesn't (and why not?)
Code: [Select]
purchases[i].Count := purchases[i].Count - numCovered;

// Error: Argument cannot be assigned to

In the former case the compiler does not correctly catch that it shouldn't allow the assignment considering its forbidding it in the second case. I think there is a bug report about that already (though it could be about a similar problem when using a with-clause on the property).

 

TinyPortal © 2005-2018