Forum > General

Can Absolute be used on string / packed record

<< < (3/17) > >>

howardpc:
The following is somewhat over-engineered, perhaps it could be simplified, however I think it achieves what you are after using Pascal union-type records rather than absolute, utilising so-called variant parts. However I hesitate to use the word "variant" since it can potentially and confusingly have several incompatible meanings.


--- 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";}};} ---program SplitShortStringsInRecord; {$mode objfpc}{$H+}{$ModeSwitch advancedrecords} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  LazLogger; type   TStr25 = String[25];  TStr40 = String[40];  TStr73 = String[73];   TMyTripleShortstringsRec = packed record    s73: TStr73;    s40: TStr40;    s25: TStr25;    procedure Init(a25: TStr25; a40: TStr40; a73: TStr73);    procedure PrintIndividualValues;    procedure PrintComposite;  end;   TMyPackedShortstringRecord = packed record    individuated: TMyTripleShortstringsRec;    procedure PrintComposite;  end; var  packedRec: TMyPackedShortstringRecord; procedure TMyPackedShortstringRecord.PrintComposite;begin  individuated.PrintComposite;end; procedure TMyTripleShortstringsRec.Init(a25: TStr25; a40: TStr40; a73: TStr73);begin  Self := Default(TMyTripleShortstringsRec);  s25 := a25;  s40 := a40;  s73 := a73;end; procedure TMyTripleShortstringsRec.PrintComposite;begin  DebugLn('composite: "%s%s%s"', [s73, s40, s25]);end; procedure TMyTripleShortstringsRec.PrintIndividualValues;begin  DebugLn('s25: "%s"', [s25]);  DebugLn('s40: "%s"', [s40]);  DebugLn('s73: "%s"', [s73]);end; begin  packedRec.individuated.Init('test s25', 'test s40 which is a bit longer', 'test s75, the longest shortstring that this record contains');  DebugLnEnter(['  PrintComposite', '']);  packedRec.PrintComposite;  DebugLn;  DebugLnEnter(['', '  PrintIndividualValues']);  packedRec.individuated.PrintIndividualValues;  DebugLn;  DebugLnExit(['', 'Press Enter to finish']);  ReadLn;end.

MarkMLl:

--- Quote from: jamie on June 29, 2022, 12:53:24 am ---S is a short string so it should allow this but I get an internal compiler error.

--- End quote ---

Why should it allow it? I don't think it's reasonable to apply absolute to an element in the middle of what is effectively an array, although I agree that an explicit error would be in order.

MarkMLl


y.ivanov:

--- Quote from: andresayang on June 27, 2022, 11:06:43 pm ---@Bart:
Yep, this is what I realize when I was remind that short string have 1 byte more (byte 0) for length.

So do you have an optimized technique to do "column separated values" of text file or we absolutely need to use Copy(S, start, length) ?

Thanks

--- End quote ---

Why you didn'd just go with PChar's (ASCIIZ) strings and replace commas with #0? (Hope it is a single-byte encoded string)

PascalDragon:

--- Quote from: jamie on June 29, 2022, 12:53:24 am ---what find interesting is this generating an internal error!


--- 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";}};} ---procedure TForm1.Button1Click(Sender: TObject);Var  S:string[100];  A:Char absolute S[1];begin  //end;       
S is a short string so it should allow this but I get an internal compiler error.

--- End quote ---

Please report as a bug with a self contained example (without LCL dependencies). Whether this should be allowed or not is up to discussion, but an internal error definitely shouldn't happen.

Warfley:
I know that I'm beating a dead horse here, but "absolute" is terible and should be always avoided.  The reason for this is that absolute circumvents any typechecking

E.g.

--- 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";}};} ---type  TTest = record    A: Integer;    B: Double  end; var  t: TTest;  a: Integer absolute t.A;  B: Double absolute t.B;begin  t.A := 42;  t.B := 3.14;  WriteLn(a, ' ', b);  ReadLn;end.Works really nice, but what if I now decide to change the type of TTest (which btw could be in a completely different unit, by a person who does not know that this code using absolute exists):

--- 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";}};} ---type  TTest = record    A: Double;    B: Double  end; var  t: TTest;  a: Integer absolute t.A;  B: Double absolute t.B;begin  t.A := 42;  t.B := 3.14;  WriteLn(a, ' ', b);  ReadLn;end.Now the program compiles fine, not a single hint or warning. But when running the result is now "0 3.14" rather than "42 3.14", which is wrong. This is the worst kind of error, one where it silently produces a wrong result without any crash, warning or even a compiler hint.

This is just an error waiting to happen. You should always write your code in a way that errors are cought as early as possible, best is if the compiler itself finds your error and does not compile. This is the complete opposite of this. In a perfect world we would have compilers that exactly know what you want to do and if you do something wrong, like the thing above, tell you "Sorry Dave, I can't let you do that". But sadly this is impossible so we as programmers need to restrict ourselves to tools that help the compiler to help us. But absolute is like telling the compiler "I don't need any help, I will never do mistakes"

For an example, if I do the same in C++, using references, which basically is the same as absolute but as active part of the typesystem it fully incorporates typechecking:

--- Code: C  [+][-]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";}};} ---    struct {        int A;        double B;    } t;    int &a = t.A;    double &b = t.B;        t = { .A = 42, .B = 3.14 };        std::cout << a << " " << b << std::endl;}Changing the A field to double will cause a compiler error, which is good, because accessing an double as an integer is most probably going to be an error.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version