Re: [fpc-pascal] For..in enumerator for generic records?

2016-12-06 Thread Sven Barth
On 02.12.2016 08:22, Ryan Joseph wrote:
> Yes but the problem is doing this with generics. :) That’s where I got stuck 
> and I don’t think it’s possible.

Ah, yes, right, sorry. Attached is a corrected version :)

Regards,
Sven

program trecenum;

{$mode objfpc}
{$modeswitch advancedrecords}

type
  generic TRec = record
  public type
TArrayType = array of T;

TEnumerator = object
private
  fArr: TArrayType;
  fIndex: Integer;
  function GetCurrent: T;
public
  constructor Create(aArr: TArrayType);
  function MoveNext: Boolean;
  property Current: T read GetCurrent;
end;

  public
arr: TArrayType;

function GetEnumerator: TEnumerator;
  end;


constructor TRec.TEnumerator.Create(aArr: TArrayType);
begin
  fArr := aArr;
  fIndex := -1;
end;

function TRec.TEnumerator.GetCurrent: T;
begin
  Result := fArr[fIndex];
end;

function TRec.TEnumerator.MoveNext: Boolean;
begin
  Inc(fIndex);
  Result := fIndex < Length(fArr);
end;

function TRec.GetEnumerator: TRec.TEnumerator;
begin
  Result.Create(Self.arr);
end;

type
  TRecLongInt = specialize TRec;
  TRecString = specialize TRec;

var
  rl: TRecLongInt;
  rs: TRecString;
  i: LongInt;
  s: String;
begin
  rl.arr := TRecLongInt.TArrayType.Create(42, 53, 96, 29);
  rs.arr := TRecString.TArrayType.Create('Alpha', 'Beta', 'Gamma', 'Delta');

  for i in rl do
Writeln(i);

  for s in rs do
Writeln(s);
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-12-01 Thread Ryan Joseph
Yes but the problem is doing this with generics. :) That’s where I got stuck 
and I don’t think it’s possible.

> On Dec 2, 2016, at 4:58 AM, Sven Barth  wrote:
> 
> I've attached an example that shows how this *can* be done (it's not the
> only way to do this however).

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-12-01 Thread Sven Barth
On 30.11.2016 12:36, Ryan Joseph wrote:
> 
> 
>> On Nov 30, 2016, at 6:24 PM, Graeme Geldenhuys 
>>  wrote:
>>
>> What would you iterate/enumerate in that?  I can understand iterating an
>> "array of TMyRec", but not TMyRec itself.
> 
> I have a dynamic array inside a record. I’m using a record instead of a class 
> because I want it to be stored on the stack. It’s trivial to just enumerate 
> the array inside the record but I wanted to try the more elegant solution of  
> making an enumerator for the record.

I've attached an example that shows how this *can* be done (it's not the
only way to do this however).

Regards,
Sven

program trecenum;

{$mode objfpc}

type
  TLongIntArray = array of LongInt;

  TRec = record
arr: TLongIntArray;
  end;

  TRecEnumerator = object
  private
fArr: TLongIntArray;
fIndex: Integer;
function GetCurrent: LongInt;
  public
constructor Create(aArr: TLongIntArray);
function MoveNext: Boolean;
property Current: LongInt read GetCurrent;
  end;

constructor TRecEnumerator.Create(aArr: TLongIntArray);
begin
  fArr := aArr;
  fIndex := -1;
end;

function TRecEnumerator.GetCurrent: LongInt;
begin
  Result := fArr[fIndex];
end;

function TRecEnumerator.MoveNext: Boolean;
begin
  Inc(fIndex);
  Result := fIndex < Length(fArr);
end;

operator enumerator(r: TRec): TRecEnumerator;
begin
  Result.Create(r.arr);
end;

var
  r: TRec;
  i: LongInt;
begin
  r.arr := TLongIntArray.Create(42, 53, 96, 29);

  for i in r do
Writeln(i);
end.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread leledumbo
> I just tried this really quick and it doesn’t seem to work with records but
if I changed it to a class that worked.

Just as you can't declare next field to be a record when learning linked
list (well, we normally learned that some time in the past), the solution is
just the same here: declare and use pointer to the record instead. Using
classes, however, the pointer can be abstracted and you will have less messy
code.




--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/For-in-enumerator-for-generic-records-tp5726980p5726995.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Mattias Gaertner
On Wed, 30 Nov 2016 18:24:11 +0700
Ryan Joseph  wrote:

>[...] I can wrap my head around nested types in classes because they’re 
>necessary for generics I just learned but nested classes feel messy. Is there 
>at least some namespace protection?

Yes.
And it follows visibility rules.

> That would be a benefit I suppose if the class was only ever used
> inside that class but a unit may be a better option in that case.

Mattias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Ryan Joseph


> On Nov 30, 2016, at 6:19 PM, Graeme Geldenhuys 
>  wrote:
> 
> Yeah, those were introduced in FPC 3.0.0 I believe - more Delphi
> compatibility changes. I still don’t know if I like it though, but
> understand its usage is meant if two classes are very close related. You
> can now have nested classes (god knows how deep), constants, vars etc
> all inside a class definition. The Delphi version of the Object Pascal
> language is becoming pretty messy.

Big mess indeed. I can wrap my head around nested types in classes because 
they’re necessary for generics I just learned but nested classes feel messy. Is 
there at least some namespace protection? That would be a benefit I suppose if 
the class was only ever used inside that class but a unit may be a better 
option in that case.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Ryan Joseph

> On Nov 30, 2016, at 6:22 PM, Marco van de Voort  wrote:
> 
> * (for..in) Enumerators in records

Where’s an example of this? I’m trying it with a generic record and I can’t it 
to compile so I’m not sure if this is supported or not.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Ryan Joseph


> On Nov 30, 2016, at 6:24 PM, Graeme Geldenhuys 
>  wrote:
> 
> What would you iterate/enumerate in that?  I can understand iterating an
> "array of TMyRec", but not TMyRec itself.

I have a dynamic array inside a record. I’m using a record instead of a class 
because I want it to be stored on the stack. It’s trivial to just enumerate the 
array inside the record but I wanted to try the more elegant solution of  
making an enumerator for the record.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Graeme Geldenhuys
On 2016-11-30 11:22, Marco van de Voort wrote:
> D2006/ FPC 2.6.0


Thanks for the correction Marco. Wow, that long ago already.


Regards,
  Graeme

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Graeme Geldenhuys
On 2016-11-30 04:03, Ryan Joseph wrote:
> I have a generic record I would like to enumerate using for..in loops.

Maybe generics are redefining this, but a record (as I understand it) is
something like

   TMyRec = record
 StrField: string;
 IntField: integer;
 BoleanField: boolean;
   end;

What would you iterate/enumerate in that?  I can understand iterating an
"array of TMyRec", but not TMyRec itself.


Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Marco van de Voort
In our previous episode, Graeme Geldenhuys said:
> Yeah, those were introduced in FPC 3.0.0 

D2006/ FPC 2.6.0

from 2.6.0 release manifest:
http://forum.lazarus.freepascal.org/index.php?topic=15656.0

* Delphi compatibility mode improvements
* Nested types, class variables and class local constants
* Advanced records syntax (no constructors yet)
* (for..in) Enumerators in records
* Class and record helpers
* Generic records, arrays and procedural types
* Delphi-compatibility of generics improved
* Scoped enumerations
* Custom messages for "deprecated" directive
* Ability to use "&" for escaping keywords
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Graeme Geldenhuys
On 2016-11-30 10:53, Ryan Joseph wrote:
> There’s a class nested inside a class?? I never saw that before.

Yeah, those were introduced in FPC 3.0.0 I believe - more Delphi
compatibility changes. I still don’t know if I like it though, but
understand its usage is meant if two classes are very close related. You
can now have nested classes (god knows how deep), constants, vars etc
all inside a class definition. The Delphi version of the Object Pascal
language is becoming pretty messy.

Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Ryan Joseph
There’s a class nested inside a class?? I never saw that before. What are all 
the inlines for btw?

> On Nov 30, 2016, at 3:09 PM, Sven Barth  wrote:
> 
> Take a look at packages/fcl-stl/src/gvector.pp, it's a generic vector 
> container that provides an enumerator.
> 
> 

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Ryan Joseph
I just tried this really quick and it doesn’t seem to work with records but if 
I changed it to a class that worked. Maybe I should use an object instead so 
it’s stored on the stack like a record? Not very important it can enumerate but 
I wanted to try first.

> On Nov 30, 2016, at 5:53 PM, Ryan Joseph  wrote:
> 
> There’s a class nested inside a class?? I never saw that before. What are all 
> the inlines for btw?

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread Sven Barth
Am 30.11.2016 05:35 schrieb "Ryan Joseph" :
>
> I have a generic record I would like to enumerate using for..in loops. Is
this even possible? I tried briefly and it wasn’t clear this was possible
so I thought I’d ask first. Thanks.
>

Take a look at packages/fcl-stl/src/gvector.pp, it's a generic vector
container that provides an enumerator.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] For..in enumerator for generic records?

2016-11-30 Thread leledumbo
> I have a generic record I would like to enumerate using for..in loops. Is
this even possible? I tried briefly and it wasn’t clear this was possible so
I thought I’d ask first. Thanks. 

Take a look at how gvector implements it:
http://svn.freepascal.org/svn/fpc/tags/release_3_0_0/packages/fcl-stl/src/gvector.pp



--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/For-in-enumerator-for-generic-records-tp5726980p5726981.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal