Forum > General

[SOLVED] Help with TVector (unit gvector)

(1/1)

Hi im Pascal:
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: ---purchases[i].Count -= numCovered;

--- End code ---

and this doesn't (and why not?)

--- Code: ---purchases[i].Count := purchases[i].Count - numCovered;

// Error: Argument cannot be assigned to

--- End code ---

I also had tried:

--- Code: ---purchases[i]^.Count -= numCovered;

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

--- End code ---

But I cannot figure out how to make FPC use this access method (gvector source):

--- Code: ---property Mutable[i : SizeUInt]: PT read getMutable;

--- End code ---

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


--- Code: ---type
TStockTransaction = record
    StockID : String;
    Basis : Real;
    Count : Real;
    Date : TDateTime;
  end;

type
  TTransactionVector = specialize TVector<TStockTransaction>;

--- End code ---

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!

zamtmn:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---purchases.mutable[i]^.Count -= numCovered;

Hi im Pascal:
OMG I'm so stupid, it's really THAT simple:

--- Code: ---purchases.Mutable[i]^.Count -= numCovered;

--- End code ---

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

But then howcome does this work the same:

--- Code: ---ShowMessage(FloatToStr(purchases[i].Count));
ShowMessage(FloatToStr(purchases.Items[i].Count));

--- End code ---

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.

--- End quote ---
http://docwiki.appmethod.com/appmethod/1.13/topics/en/Properties

Great site...

PascalDragon:

--- Quote from: Hi im Pascal on June 15, 2021, 10:34:50 pm ---I'm a little sad, and maybe a little shocked...

howcome this compiles (and doesn't do what it looks to do):

--- Code: ---purchases[i].Count -= numCovered;

--- End code ---

and this doesn't (and why not?)

--- Code: ---purchases[i].Count := purchases[i].Count - numCovered;

// Error: Argument cannot be assigned to

--- End code ---

--- End quote ---

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).

Navigation

[0] Message Index

Go to full version