On 28/04/17 04:30, Ryan Joseph wrote:
Instead of making constructors and doing busy work It would be nice if Free 
Pascal could let you assign records outside of type blocks like:
rec := (x: 0; y: 0; z: 0);
Why isn’t this possible btw? I saw some C++ code do this and it seems like an 
obvious solution that should have existed 20 years ago. The feature exists for 
type blocks so why not just enable it for other parts of code?
I had another idea to make this a little simpler by using open arrays and 
operator overloading. The compiler doesn’t permit this however. Is it a bug, my 
code or just a limitation?

Curiously, I was up against exactly the same thing yesterday when setting up an array for GetLongOpts() and a table for tokenising some other text elements.

I ended up using a dynamic array and overloading + for the first of those since it allowed me to do

  with option do begin
    name := 'block';
    value := name[1]   <===
  end;
  theOpts += option;

which wouldn't be available at compilation time, and also since build-time directives could easily include/exclude options. For the other one I used an array of records.

The problem with arrays of records as I see it is that the number of elements has to be predefined:

const   TopMatchItem= 10;

type    TMatchItem= record
                      name: string;
                      field: TCSVField
                    end;
        TAMatchItem= array[0..TopMatchItem] of TMatchItem;

const   matchItems: TAMatchItem= ((name: ''; field: CSVBad),
                      (name: 'block'; field: CSVBlock),
..

Or from the FPC compiler itself:

  ttokenarray=array[ttoken] of tokenrec;
  ptokenarray=^ttokenarray;
..

const
  arraytokeninfo : ttokenarray =(
      (str:''              ;special:true ;keyword:[m_none];op:NOTOKEN),
..

Is there any way that the length of an array being used for that sort of job can be defined by what's put into it, rather than having to be predefined?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to