Recent

Author Topic: objects with variant part  (Read 3263 times)

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
objects with variant part
« on: October 03, 2017, 10:18:50 pm »
Why can't objects have variant parts?

Code: Pascal  [Select][+][-]
  1. myObj = object
  2.                 case foo: byte of
  3.                         …
  4.         end;
works as a record only. I thought “Objects should be treated as a special kind of record.” (Chapter on “objects” in the reference manual)
Yours Sincerely
Kai Burghardt

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: objects with variant part
« Reply #1 on: October 04, 2017, 08:07:15 am »
Why can't objects have variant parts?
I can't really think of any technical reason, so I guess it's down to compatibility only, as object can have a field of type variant record, which should be the same in the end (minus the longer access).

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: objects with variant part
« Reply #2 on: October 04, 2017, 12:41:01 pm »
It is perfectly possible (and the entry point will be the same for object and record.)
The correct syntax however is to declare a record inside the object, like:
Code: Pascal  [Select][+][-]
  1. program program1;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. type
  4.   foo = object
  5.     bar:record
  6.     case EyeColor: (Red, Green, Blue, Brown) of
  7.       Red, Green : (WearsGlasses: Boolean);
  8.       Blue, Brown: (LengthOfLashes: Integer);
  9.     end;
  10.  end;
  11. begin
  12. end.
The record can subsequently contain variant parts if required as you see here.
Contrived example, any use for it?  O:-) 8-) (Parts of it are from the GNU Pascal manual...)
« Last Edit: October 04, 2017, 01:12:58 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: objects with variant part
« Reply #3 on: October 04, 2017, 02:29:09 pm »
[…]I can't really think of any technical reason, so I guess it's down to compatibility only, as object can have a field of type variant record, which should be the same in the end (minus the longer access).
It is perfectly possible (and the entry point will be the same for object and record.)
The correct syntax however is to declare a record inside the object, like:[…]
That introduces another level I wanted to avoid.
Code: Pascal  [Select][+][-]
  1. var
  2.         subject: foo;
  3. begin
  4.         subject.bar.eyeColor := red;
  5.         // but simply
  6.         //subject.eyeColor := red;
  7.         // does not work
  8. end.
so a variant part per se can't be a member of an object.

It'd been nice, if I could write myObject.convertToBlub(), but I'm OK with convertToBlub(myRecord). I really don't like to have longer access paths.
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: objects with variant part
« Reply #4 on: October 04, 2017, 04:42:31 pm »
Kai, it is not another level of indirection. You did not use  the correct syntax. But syntax can be discussed on the mailing list.... (Good luck!)
Also note that work is being done - in trunk+ -  on the subject of default properties.... That would solve it...
« Last Edit: October 04, 2017, 04:54:53 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9874
  • Debugger - SynEdit - and more
    • wiki
Re: objects with variant part
« Reply #5 on: October 04, 2017, 05:05:09 pm »
If you just need procedures, then use advanced records (search forum / wiki)

"object" is only needed, if you want a record like structure with inheritance.

 

TinyPortal © 2005-2018