Recent

Author Topic: [Solved] operator overload for an instance-less type  (Read 3499 times)

guest58172

  • Guest
[Solved] operator overload for an instance-less type
« on: January 10, 2017, 11:46:05 pm »
Is it possible ? I use a record that only contains static class functions, in other words it's just used to encapsulate data and have methods callable in an IDE friendly way.

Code tsantzas:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$Mode objfpc}
  3. {$ModeSwitch advancedrecords}
  4. {$Assertions+}
  5.  
  6. type
  7.   TFoo = record
  8.     class function test(i: integer): boolean; static;
  9.   end;
  10.  
  11.   class function TFoo.test(i: integer): boolean; static;
  12.   begin
  13.     Result := true;
  14.   end;
  15.  
  16.   operator in(Lhs: integer; var Rhs: TFoo): boolean;
  17.   begin
  18.     Result := Rhs.test(Lhs);
  19.   end;
  20.  
  21. begin
  22.   assert(TFoo.test(42));
  23.   assert(42 in TFoo);
  24. end.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: operator overload for an instance-less type
« Reply #1 on: January 11, 2017, 12:17:11 am »
Well the error is that this expects a variable, so is easy to solve:

Code: Pascal  [Select][+][-]
  1. var
  2.   TmyFoo: TFoo;
  3. ...
  4.   assert(42 in TmyFoo);

In other languages there's an instance of the static class?
« Last Edit: January 11, 2017, 12:18:53 am by lainz »

guest58172

  • Guest
Re: operator overload for an instance-less type
« Reply #2 on: January 11, 2017, 12:45:02 am »
Ok so not possible...(about other languages, yes, and this works when operators are implemented as members functions, with the Pascal operator overloading syntax you see the limit: it's just impossible to specify a type as parameter !!)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: operator overload for an instance-less type
« Reply #3 on: January 11, 2017, 07:27:14 am »
Ok so not possible...(about other languages, yes, and this works when operators are implemented as members functions, with the Pascal operator overloading syntax you see the limit: it's just impossible to specify a type as parameter !!)
But it is possible... as long the type is a class you can specify it as a class reference....
Example:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. type
  4.   TMyClass = class
  5.   class function test(i: integer): boolean; static;
  6.   end;
  7.  
  8.   TMyClassRef = class of TMyClass;
  9.  
  10.   class function TMyClass.test(i: integer): boolean; static;
  11.   begin
  12.     result := true;
  13.     writeln(i);
  14.   end;
  15.  
  16.   procedure testme(a:TMyClassRef);
  17.   begin
  18.     a.test(100);
  19.   end;  
  20. begin
  21.   testme(TmyClass); //prints 100 ;)
  22. end.
  23.  

Note that in other languages that is about the same. E.g. that it works in Java is because there everything is an object (class), even simple types.
So nothing is missing. Even if you would need reflection you can use RTTI. (which is about the same as in Java too)
If you need it, then box your type into a class....If it isn't a class yet ;)

Your example adapted:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$Mode objfpc}
  3. {$Assertions+}
  4.  
  5. type
  6.   TFoo = class
  7.     class function test(i: integer): boolean; static;
  8.   end;
  9.   TFooRef = class of TFoo;
  10.    
  11.   class function TFoo.test(i: integer): boolean; static;
  12.   begin
  13.     Result := true;
  14.   end;
  15.  
  16.   operator in(Lhs: integer; const Rhs: TFooRef): boolean;
  17.   begin
  18.     Result := Rhs.test(Lhs);
  19.   end;
  20.  
  21. begin
  22.   assert(TFoo.test(42));
  23.   assert(42 in TFoo);
  24. end.
  25.  

So, yes, it is definitely possible to specify a type as parameter!! As long as it's a class...
I am rather pleased with that example ;) 8-) 8)
« Last Edit: January 11, 2017, 08:53:20 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

guest58172

  • Guest
Re: operator overload for an instance-less type
« Reply #4 on: January 11, 2017, 12:30:36 pm »
Yes, good find! Gotta check if data are well all stack allocated (e.g there'll be several private const array) but I'm 99% sure that it's okay.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [Solved] operator overload for an instance-less type
« Reply #5 on: January 11, 2017, 12:48:00 pm »
@BBasile
Let me know plz. Your example code now works with my small changes. If it does not work with more complex classes, there may be a bug...
Class references are supposed to work as I explained.

But I am still bemused if I can come up with an actual use for your example  :D
« Last Edit: January 11, 2017, 12:51:00 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

guest58172

  • Guest
Re: [Solved] operator overload for an instance-less type
« Reply #6 on: January 11, 2017, 04:19:28 pm »
Initially I asked the Question because of
- perfect hash set with source code generator:
https://github.com/BBasile/IsItThere/blob/master/src/formaters/pascal.d
- that render for example
https://github.com/BBasile/Coedit/blob/master/src/ce_dlangmaps.pas

Such hash sets need to be fast. In no way data must be retrieved from the heap !
So If I can get the "in" operator it's good but I could live without.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: [Solved] operator overload for an instance-less type
« Reply #7 on: January 11, 2017, 05:00:36 pm »
The heap is not necessarily slower in the case of a class: the compiler will take a reference location and the cpu will pipe-line. (just like the VMT is always a reference)
So you should test. The compiler (and the cpu) is much smarter than you think! It used to be more of an issue many moons ago. The stack is not the holy grail.

But in that case, that is not the subject. You should have followed compiler classes ;) And examine assembler output on this kind of low-level.
« Last Edit: January 11, 2017, 05:38:22 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018