Sign in Join

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

Mailing lists / fpc-pascal / 2012-11

Errors using Extended Class Syntax

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

bsquared

Hello,

I followed the example on the wiki page, but the compiler disallowed both of the following attempts.

{$mode objfpc}{$H+}
TExample1 = class
private
   const
     CN_CONST = 'EXAMPLE CONSTANT';
published
   property Example: string
     read CN_CONST;
end;

Error: Unknown class field or method identifier "CN_WELCOME"

TExample2 = class
private
   fExample: string;
   const
     CN_CONST = 'EXAMPLE CONSTANT';
published
   property Example: string
     read fExample
     write fExample
     default CN_CONST;
end;

Error: Property can't have a default value

I can work around this by using a getter method which can read the private constant field, but I am curious why these attempts did not work.

Also, What is the static keyword on the class method?
class procedure SetSomeClassVar(const AValue: TSomeType); static;
$ fpc
Free Pascal Compiler version 2.7.1 [2012/11/03] for i386

Thank you.

Regards, Brian

Reply by email

Jonas Maebe
to bsquared
bsquared wrote
I followed the example on the wiki page, but the compiler disallowed both of the following attempts.

On which wiki page did you find this code?

Show quoted text 12 lines
{$mode objfpc}{$H+} TExample1 = class private const CN_CONST = 'EXAMPLE CONSTANT'; published property Example: string read CN_CONST; end; Error: Unknown class field or method identifier "CN_WELCOME"

Properties can only read via methods or fields, not via constants. A constant is not the same as a field. Does Delphi allow the above?

Show quoted text 13 lines
TExample2 = class private fExample: string; const CN_CONST = 'EXAMPLE CONSTANT'; published property Example: string read fExample write fExample default CN_CONST; end; Error: Property can't have a default value

Properties that return strings cannot have a default value. Only immediate values that moreover fit in 32 bits can be used as default value, due to the format of the RTTI. See http://bugs.freepascal.org/view.php?id=19199 for more information.

Also, What is the static keyword on the class method? class procedure SetSomeClassVar(const AValue: TSomeType); static;

It results in the class method not getting a hidden "self" parameter, which otherwise contains the class reference on which the class method was invoked.

Jonas

Reply by email

bsquared
to Jonas Maebe
Jonas Maebe wrote 6 lines
On 24 Nov 2012, at 17:55, bsquared wrote: > I followed the example on the wiki page, but the compiler disallowed both of the following attempts. On which wiki page did you find this code?

The code on the wiki does conform to your helpful comments. http://wiki.freepascal.org/extended_class_syntax

Show quoted text 42 lines
> {$mode objfpc}{$H+} > > TExample1 = class > private > const > CN_CONST = 'EXAMPLE CONSTANT'; > published > property Example: string > read CN_CONST; > end; > > Error: Unknown class field or method identifier "CN_WELCOME" Properties can only read via methods or fields, not via constants. A constant is not the same as a field. Does Delphi allow the above? > TExample2 = class > private > fExample: string; > const > CN_CONST = 'EXAMPLE CONSTANT'; > published > property Example: string > read fExample > write fExample > default CN_CONST; > end; > > Error: Property can't have a default value Properties that return strings cannot have a default value. Only immediate values that moreover fit in 32 bits can be used as default value, due to the format of the RTTI. See http://bugs.freepascal.org/view.php?id=19199 for more information. > Also, What is the static keyword on the class method? > class procedure SetSomeClassVar(const AValue: TSomeType); static; It results in the class method not getting a hidden "self" parameter, which otherwise contains the class reference on which the class method was invoked. Jonas_______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Thank you for the clarifications.

Regards, Brian

Reply by email