Recent

Author Topic: [SOLVED] Shorthand for assigning to record variable?  (Read 7254 times)

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
[SOLVED] Shorthand for assigning to record variable?
« on: July 29, 2016, 12:56:33 pm »
I have a dynamic array of records and I'm adding a new one like this:
Code: [Select]
        SetLength(mainList, length(mainList) + 1);
        mainList[high(mainList)].x := x;
        mainList[high(mainList)].y := y;
        mainList[high(mainList)].v := v;

It's no so bad with only three record 'members' but it would be a bit tedious with more. Is there a shorthand way to achieve this? I don't like the idea of 'with..do' and don't want more overhead by putting creating a procedure/function to do it. Is there a form something like this, which is supported in some other languages?

Code: [Select]
mainList[high(mainList)] = (1, 2, 3);

« Last Edit: July 29, 2016, 05:15:18 pm by mrguzgog »

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Shorthand for assigning to record variable?
« Reply #1 on: July 29, 2016, 01:09:20 pm »
A dynamic array of records is the wrong structure in this case, because adding elements leads to a reallocation which is really, really expensive.
A list or something is a far better structure to implement such things. But to answer your question:

Expand your record with a SetValues function like:
Code: Pascal  [Select][+][-]
  1. program insertitems;
  2. {$mode delphi}
  3. type
  4.   TItem = record
  5.     x,y,v:integer;
  6.    function SetValues(a,b,c:integer):TItem;
  7.   end;
  8.  
  9.   function Titem.SetValues(a,b,c:integer):TItem;
  10.   begin
  11.     x:=a;
  12.     y:=b;
  13.     v :=c;
  14.    Result := Self;
  15.   end;
  16.  
  17. var
  18.   it:Titem;
  19.   Mainlist:Array of TItem;
  20. begin
  21.   Setlength(MainList, Length(Mainlist)+1);
  22.   MainList[High(MainList)] := it.SetValues(1,2,3);
  23. end.
  24.  
« Last Edit: July 29, 2016, 01:48:04 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Shorthand for assigning to record variable?
« Reply #2 on: July 29, 2016, 01:12:29 pm »
No. There is only with.

In the case of records entirely made of constants you can do

Code: Pascal  [Select][+][-]
  1. const  somename : TRecordType = (x:1;y:2,z:3);

and do

Code: Pascal  [Select][+][-]
  1.    mainlist [ind]:=somename

The syntax you propose is dangerous. At the very least all values should be tagged with their identifiers (like in the somename declaration above)

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Shorthand for assigning to record variable?
« Reply #3 on: July 29, 2016, 01:52:34 pm »
No. There is only with.

@Marco:  my answer crossed yours , but of course there are advanced records as per the above example...
@mrguzgog: A list is probably a better structure unless you definitely need array order since it does a lot less reallocations.

Maybe this works for you:
Code: Pascal  [Select][+][-]
  1. program listtest;
  2. {$MODE DELPHI}
  3. uses glinkedlist;  // from package flc-stl;
  4.  
  5. type
  6.   Titem = record
  7.   x,y,v:integer;
  8.   function SetValues(const a,b,c:integer):Titem;
  9.   end;
  10.  
  11.   function Titem.SetValues(const a,b,c:integer):Titem;
  12.   begin
  13.     x := a;
  14.     y := b;
  15.     v := c;
  16.     Result := self;
  17.   end;
  18.  
  19. var
  20.  R:Titem;
  21.  MainList:Tlinkedlist<Titem>;
  22. begin
  23.  mainlist :=Tlinkedlist<Titem>.create;
  24.  try
  25.    mainList.InsertLast(R.SetValues(1,2,3));
  26.   // process some more, see glinkedlist
  27.  finally
  28.    mainlist.free;
  29.  end;
  30. end.
  31.  
  32.  
« Last Edit: July 29, 2016, 02:02:02 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
Re: Shorthand for assigning to record variable?
« Reply #4 on: July 29, 2016, 02:06:34 pm »
Thaddy, advanced records are fine for implementing such shorthands for the few cases where it is limiting, but why do you return a copy of that record ?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Shorthand for assigning to record variable?
« Reply #5 on: July 29, 2016, 02:10:03 pm »
don't want more overhead by putting creating a procedure/function to do it
inline procedure does not incure any overhead, apart from you need to write it.
Thaddy, advanced records are fine for implementing such shorthands for the few cases where it is limiting, but why do you return a copy of that record ?
He might have forgotten that records are value types :P

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Shorthand for assigning to record variable?
« Reply #6 on: July 29, 2016, 02:26:37 pm »
He might have forgotten that records are value types :P

The code is correct for its purpose. The copy is intentional. No leaks, no worries, behavior as expected.
But that list has some limitations I see.
I will write a better example based on the same structure.
« Last Edit: July 29, 2016, 02:38:33 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Shorthand for assigning to record variable?
« Reply #7 on: July 29, 2016, 03:26:06 pm »
A dynamic array of records is the wrong structure in this case, because adding elements leads to a reallocation which is really, really expensive.
A list or something is a far better structure to implement such things.

Ok thanks. I'm familiar with ArrayList in Java and this looks pretty similar. I was doing it like this to get familiar with the way dynamic arrays work in fp.

I hadn't got as far as reading about 'advanced records' and wasn't aware that they could have methods - I like that, rather like D's structs. The record/object/class business seems a bit confusing but it seems I could just ignore objects (I mean 'object type' not instances of a class which are what I think of as objects!).

@marcov: Yes, I don't like that 'dangerous' syntax, it's all too easy to omit or misplace a value - the form you suggest with tags is something I've encountered before.

Thanks all for the replies.

P.S. Is there any downside to compiling in Delphi mode? Does standard fp code still work without modification provided I take account of the things specified here http://www.freepascal.org/docs-html/prog/progse74.html ?

EDIT. Hmm, 'uses glinkedlist' gives me a 'can't find unit' error and I don't see it here: http://wiki.freepascal.org/Data_Structures,_Containers,_Collections
Actually as I only need to add items on the end a vector will do - and that compiles ok. :D

« Last Edit: July 29, 2016, 03:35:15 pm by mrguzgog »

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Shorthand for assigning to record variable?
« Reply #8 on: July 29, 2016, 03:39:37 pm »
P.S. Is there any downside to compiling in Delphi mode? Does standard fp code still work without modification provided I take account of the things specified here http://www.freepascal.org/docs-html/prog/progse74.html ?

Well, no, you can mix and match on a per unit basis.
I prefer mode delphi because I also work a lot with delphi, but mode objfpc is more strict. Some prefer that.

glinkedlist is in package fcl-stl; You need a recent fpc version for that.
« Last Edit: July 29, 2016, 04:29:12 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Shorthand for assigning to record variable?
« Reply #9 on: July 29, 2016, 03:55:08 pm »
P.S. Is there any downside to compiling in Delphi mode?
No, other than Delphi restriction for certain features applies
Does standard fp code still work without modification provided I take account of the things specified here http://www.freepascal.org/docs-html/prog/progse74.html ?
Yes
EDIT. Hmm, 'uses glinkedlist' gives me a 'can't find unit' error and I don't see it here: http://wiki.freepascal.org/Data_Structures,_Containers,_Collections
Actually as I only need to add items on the end a vector will do - and that compiles ok. :D
There's no glinkedlist, check your units directory instead of imagining whether a unit exists or not

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Shorthand for assigning to record variable?
« Reply #10 on: July 29, 2016, 04:07:47 pm »
There's no glinkedlist, check your units directory instead of imagining whether a unit exists or not

From Thaddy's example code:
Code: [Select]
uses glinkedlist;  // from package flc-stl;
I assumed that glinkedlist is part of flc-stl but it seems not. However GVector is and it compiles fine. Is glinkedlist something I'd need to manually add to my installation if I wanted to use it? I can't find any docs for GVector other than http://www.ioinformatics.org/oi/pdf/INFOL101.pdf which is just an outline but guessing from C++ docs works so far :D

I have another question that probably belongs in its own thread, but, as I mentioned it above...

Code: [Select]
type Location = record
...
end;
var
somevec:        TVector<Location>;

...works when compiling in {$MODE DELPHI} but  {$MODE OBJFPC} gives me ' Error: Generics without specialization cannot be used as a type for a variable'. Why is that?


Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Shorthand for assigning to record variable?
« Reply #11 on: July 29, 2016, 04:28:55 pm »
Is glinkedlist something I'd need to manually add to my installation if I wanted to use it?
No. It's something you have to CREATE, because it doesn't exist ATM.
I can't find any docs for GVector other than http://www.ioinformatics.org/oi/pdf/INFOL101.pdf which is just an outline but guessing from C++ docs works so far :D
The distributed FPC probably doesn't ship with fcl-stl docs. The source, however, does: http://svn.freepascal.org/svn/fpc/tags/release_3_0_0/packages/fcl-stl/doc/main.pdf
I have another question that probably belongs in its own thread, but, as I mentioned it above...

...works when compiling in {$MODE DELPHI} but  {$MODE OBJFPC} gives me ' Error: Generics without specialization cannot be used as a type for a variable'. Why is that?
Read the documentation, the two modes implement different generics syntax.

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: Shorthand for assigning to record variable?
« Reply #12 on: July 29, 2016, 04:31:51 pm »
There's no glinkedlist, check your units directory instead of imagining whether a unit exists or not
What >:(?
What a load of bull >:D
It is in packages/fcl-stl.... Maybe trunk, but it is there. It is compatible so if you need it, check it out from trunk. I thought it was back-ported to fixes.
« Last Edit: July 29, 2016, 04:37:07 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Shorthand for assigning to record variable?
« Reply #13 on: July 29, 2016, 04:47:02 pm »
There's no glinkedlist, check your units directory instead of imagining whether a unit exists or not
What >:(?
What a load of bull >:D
It is in packages/fcl-stl.... Maybe trunk, but it is there. It is compatible so if you need it, check it out from trunk. I thought it was back-ported to fixes.
Ah, yes it is in trunk:
Code: [Select]
$ svn log glinkedlist.pp
------------------------------------------------------------------------
r33311 | michael | 2016-03-22 04:01:38 +0700 (Sel, 22 Mar 2016) | 1 line

* Generic linked list class, donated by Denis Volodarsky (bug ID 24501)
------------------------------------------------------------------------

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: Shorthand for assigning to record variable?
« Reply #14 on: July 29, 2016, 04:50:33 pm »
The distributed FPC probably doesn't ship with fcl-stl docs. The source, however, does: http://svn.freepascal.org/svn/fpc/tags/release_3_0_0/packages/fcl-stl/doc/main.pdf
...
Read the documentation, the two modes implement different generics syntax.
Thank you for the link, that document actually answers all questions (how to declare TVector when using {$MODE OBJFPC}, and how to use TVector)  :D

 

TinyPortal © 2005-2018