Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-07

"class" methods for objects

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Helmut Hartl

Hello,

while converting bullet physic code to native fpc, i would find it handy to have something like "class" methods for objects. (Methods without self pointer)
(It would enhance the readability of the code much, because I were not forced to
move such c++ class functions to global functions) Reading doc and wiki I found nothing similar.

A simple test of the below showed test code gives me the following impression.
a) It's not fully implemented
b) It's not handled right in the compiler
(the code should not compile if the second writeln is commented) c) It has to be used differently.

Thanks for comments,

helmut

----snip
program Project1;
type
   TestObject=object
     class function StaticFunction:boolean;
   end;
class function TestObject.StaticFunction: boolean;
begin
   result:=true;
end;
var o:TestObject;
begin
  writeln(o.StaticFunction);
  writeln(TestObject.StaticFunction); //(22,21) Error: Only static
variables can be used in static methods or outside methods
end.
---snap
Michael Van Canneyt
to Helmut Hartl

It exists already, but is called 'static'

program testso;
Type
   MyObject = Object
     procedure MySomething; static;
   end;
Procedure MyObject.MySomething;
begin
   Writeln('OK');
end;
Var
   O : MyObject;
begin
   O.MySomething;
end.

Michael.

Helmut Hartl wrote 47 lines
Hello, while converting bullet physic code to native fpc, i would find it handy to have something like "class" methods for objects. (Methods without self pointer) (It would enhance the readability of the code much, because I were not forced to move such c++ class functions to global functions) Reading doc and wiki I found nothing similar. A simple test of the below showed test code gives me the following impression. a) It's not fully implemented b) It's not handled right in the compiler (the code should not compile if the second writeln is commented) c) It has to be used differently. Thanks for comments, helmut ----snip program Project1; type TestObject=object class function StaticFunction:boolean; end; class function TestObject.StaticFunction: boolean; begin result:=true; end; var o:TestObject; begin writeln(o.StaticFunction); writeln(TestObject.StaticFunction); //(22,21) Error: Only static variables can be used in static methods or outside methods end. ---snap _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reimar Grabowski
to Michael Van Canneyt
Show quoted text 6 lines
Var O : MyObject; begin O.MySomething; end.

Interesting that you can call them on not initialized objects. I always assumed that they had to be called in a static way:

begin
  MyObject.MySomething;
end.

Always learning something new.

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?
Jonas Maebe
to Helmut Hartl

Helmut Hartl wrote on Fri, 16 Jul 2010:

Show quoted text 5 lines
Am 16.07.10 10:48, schrieb Michael Van Canneyt: > It exists already, but is called 'static' > Thank you for clarification, in a quick test it seem to work for my use case. When is it likeley that this feature will arrive in 2.4.x ?
In 2.4.x, you have to use the -St command line parameter or add
{$static on} to your source code to enable the "static" keyword.

Jonas

---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.

Michael Van Canneyt
to Reimar Grabowski: On Fri, 16 Jul 2010 10:48:19 +0200 (CEST) Interesting that you can call them on not…
Reimar Grabowski wrote 17 lines
On Fri, 16 Jul 2010 10:48:19 +0200 (CEST) Michael Van Canneyt wrote: > Var > O : MyObject; > > begin > O.MySomething; > end. Interesting that you can call them on not initialized objects. I always assumed that they had to be called in a static way: begin MyObject.MySomething; end. Always learning something new.

One is never too old to learn :-)

Michael.

Helmut Hartl
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
> Always learning something new. > One is never too old to learn :-) Michael.

But one is always able to age faster,
while boldly going to where no man has gone
baldly before :-) (spelling intentional)

While testing with a simplified usage pattern I encountered a compiler problem.
Should I report a bug - or am I missing something ?

helmut

--snip--
program project_static2;
{$static on}
type
   Etyp=(t1,t2,t3);
   function ProxyObject_IsInSubrange(const typ:Etyp):boolean;
   begin
     ProxyObject_IsInSubrange:=typ<=t2;
   end;
type
   ProxyObject=object
     function IsInSubrange(const typ:Etyp):boolean;static;
   end;
   RealObject=object
     mytyp:Etyp;
     function IsInSubrange:boolean;
   end;
function RealObject.IsInSubrange: boolean;
begin
   IsInSubrange:=ProxyObject.IsInSubrange(mytyp);  //
project1.lpr(25,15) Error: Class isn't a parent class of the current class
   IsInSubrange:=ProxyObject_IsInSubrange(mytyp); //works but very ugly
(due to lots of similar code)
end;
function ProxyObject.IsInSubrange(const typ: Etyp): boolean;
begin
   IsInSubrange:=typ<=t2;
end;
var o:RealObject;
begin
  writeln(ProxyObject.IsInSubrange(t3));
  o.mytyp:=t3;
  writeln (o.IsInSubrange);
end.
--snap--

Output:
--- fpc 2.4.0:

BigMac:test helly$ /usr/local/lib/fpc/2.4.0/ppc386 project_static2.lpr Free Pascal Compiler version 2.4.0 [2009/12/20] for i386 Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Darwin for i386
Compiling project_static2.lpr
project_static2.lpr(22,29) Error: Class isn't a parent class of the current class
Fatal: Compilation aborted
An unhandled exception occurred at $000994AD :
EAccessViolation : Access violation
$000994AD
$001264B6
$0012A57D
$0012A58E
$0012A58E
$0012A58E
$0012ADF0
$00119AD9
$00119D89
$001118E1
$00114BAA
$0011555C
$001158B2
$0011176D
$00114BAA
$001507FC
$000310F3

--- fpc 2.5.1 + debuginfo
BigMac:test helly$ /usr/local/lib/fpc/2.5.1/ppc386 project_static2.lpr Free Pascal Compiler version 2.5.1 [2010/07/16] for i386 Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Darwin for i386
Compiling project_static2.lpr
project_static2.lpr(2,2) Warning: Illegal compiler directive "$STATIC" project_static2.lpr(22,29) Error: Class isn't a parent class of the current class
Fatal: Compilation aborted
An unhandled exception occurred at $000B2CED :
EAccessViolation : Access violation
$000B2CED TYPECHECKPASS_INTERNAL, line 70 of
/Users/helly/pascal/fpc_trunk/compiler/pass_1.pas
$0014B006 FACTOR, line 2317 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0014EB9A SUB_EXPR, line 2772 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0014EBAB SUB_EXPR, line 2774 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0014EBAB SUB_EXPR, line 2774 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0014EBAB SUB_EXPR, line 2774 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0014F261 EXPR, line 2902 of
/Users/helly/pascal/fpc_trunk/compiler/pexpr.pas
$0013CD97 STATEMENT, line 1111 of
/Users/helly/pascal/fpc_trunk/compiler/pstatmnt.pas $0013D0D9 STATEMENT_BLOCK, line 1219 of
/Users/helly/pascal/fpc_trunk/compiler/pstatmnt.pas $001346EB BLOCK, line 246 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$00137B6A TCGPROCINFO__PARSE_BODY, line 1440 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$00138516 READ_PROC, line 1753 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$00138902 READ_DECLARATIONS, line 1862 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$00134519 BLOCK, line 171 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$00137B6A TCGPROCINFO__PARSE_BODY, line 1440 of
/Users/helly/pascal/fpc_trunk/compiler/psub.pas
$0017679C COMPILE, line 403 of
/Users/helly/pascal/fpc_trunk/compiler/parser.pas
$000334A3 COMPILE, line 249 of
/Users/helly/pascal/fpc_trunk/compiler/compiler.pas

Michael Van Canneyt
to Helmut Hartl
Helmut Hartl wrote 41 lines
Am 16.07.10 12:46, schrieb Michael Van Canneyt: >> Always learning something new. >> > One is never too old to learn :-) > > Michael. But one is always able to age faster, while boldly going to where no man has gone baldly before :-) (spelling intentional) While testing with a simplified usage pattern I encountered a compiler problem. Should I report a bug - or am I missing something ? helmut --snip-- program project_static2; {$static on} type Etyp=(t1,t2,t3); function ProxyObject_IsInSubrange(const typ:Etyp):boolean; begin ProxyObject_IsInSubrange:=typ<=t2; end; type ProxyObject=object function IsInSubrange(const typ:Etyp):boolean;static; end; RealObject=object mytyp:Etyp; function IsInSubrange:boolean; end; function RealObject.IsInSubrange: boolean; begin IsInSubrange:=ProxyObject.IsInSubrange(mytyp); // project1.lpr(25,15) Error: Class isn't a parent class of the current class

This could be a bug in the compiler, yes.

Michael.