class constants

classic Classic list List threaded Threaded
32 messages Options
12
Reply | Threaded
Open this post in threaded view
|

class constants

Jonathan Benedicto
Hello,

Does FPC support class constants, and if it doesn't, are there any plans to
add this support?

Example:
type
  TMyClass = class
    begin
      private
        const MyConst = '123';

    end;

Jon

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

RE: class constants

Cox, Stuart TRAN:EX
Have a look at http://delphi.about.com/library/weekly/aa031505a.htm as a
(not too bad) way to do it in Delphi.  FPC too?


Stu Cox
Project Management Technician
Southern Interior Region
Ministry of Transportation
342-447 Columbia St.,
Kamloops, BC V2C 2T3
p: 250-828-4320
f: 250-828-4229
[hidden email]

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Jonathan
Benedicto
Sent: Wednesday, January 23, 2008 11:26 AM
To: FPC-Pascal
Subject: [fpc-pascal] class constants

Hello,

Does FPC support class constants, and if it doesn't, are there any plans
to add this support?

Example:
type
  TMyClass = class
    begin
      private
        const MyConst = '123';

    end;

Jon

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Leonardo M. Ramé
In reply to this post by Jonathan Benedicto
Jonathan,
I never needed such type of feature, a propossed solution is to create one unit per class, and
declare your constants in the implementation section of each unit.

Example:

unit People

interface

type
  TPeople = class
  ..
  end;

implementation

const
   name = 'default name';

...

end.

--- Jonathan Benedicto <[hidden email]> wrote:

> Hello,
>
> Does FPC support class constants, and if it doesn't, are there any plans to
> add this support?
>
> Example:
> type
>   TMyClass = class
>     begin
>       private
>         const MyConst = '123';
>
>     end;
>
> Jon
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Marco van de Voort
In reply to this post by Jonathan Benedicto
>
> Does FPC support class constants, and if it doesn't, are there any plans
> to add this support?

Have a look at
http://www.freepascal.org/faq.var#extensionselect
 
The question would be "what would make this possible?"
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Joao Morais
In reply to this post by Leonardo M. Ramé
Leonardo M. Ramé wrote:
> Jonathan,
> I never needed such type of feature, a propossed solution is to create one unit per class, and
> declare your constants in the implementation section of each unit.

A class function would be a better approach.


> Example:
>
> unit People
>
> interface
>
> type
>   TPeople = class
>   ..
>   end;
>
> implementation
>
> const
>    name = 'default name';
>
> ...
>
> end.
>
> --- Jonathan Benedicto <[hidden email]> wrote:
>
>> Hello,
>>
>> Does FPC support class constants, and if it doesn't, are there any plans to
>> add this support?
>>
>> Example:
>> type
>>   TMyClass = class
>>     begin
>>       private
>>         const MyConst = '123';
>>
>>     end;
>>
>> Jon
>>
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Leonardo M. Ramé
With your response I assume he meant something like "static attributes" when he said "class
constants".

--- Joao Morais <[hidden email]> wrote:

> A class function would be a better approach.
>
>



      ____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Joao Morais
Leonardo M. Ramé wrote:
> With your response I assume he meant something like "static attributes" when he said "class
> constants".

Yup. A class variable is another matter, which afaik cannot be reached
with one class per unit approach.

> --- Joao Morais <[hidden email]> wrote:
>
>> A class function would be a better approach.
>>
>>
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Joao Morais
In reply to this post by Marco van de Voort
Marco van de Voort wrote:
>> Does FPC support class constants, and if it doesn't, are there any plans
>> to add this support?
>
> Have a look at
> http://www.freepascal.org/faq.var#extensionselect
>  
> The question would be "what would make this possible?"

What about class vars?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Marco van de Voort
> Marco van de Voort wrote:
> >> Does FPC support class constants, and if it doesn't, are there any plans
> >> to add this support?
> >
> > Have a look at
> > http://www.freepascal.org/faq.var#extensionselect
> >  
> > The question would be "what would make this possible?"
>
> What about class vars?

Same answer.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Jonathan Benedicto
In reply to this post by Marco van de Voort
Marco van de Voort wrote:
>>
>> Does FPC support class constants, and if it doesn't, are there any plans
>> to add this support?
>
> Have a look at
> http://www.freepascal.org/faq.var#extensionselect
>
> The question would be "what would make this possible?"

Hi Marco,

I'm not sure what you mean - do you mean would I be willing to write the
support myself?

Jon

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Henry Vermaak
On 24/01/2008, Jonathan Benedicto <[hidden email]> wrote:
>
> I'm not sure what you mean - do you mean would I be willing to write the
> support myself?

i think he's asking what the use of it is.  number 2 under the link he gave.

henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Jonathan Benedicto
Henry Vermaak wrote:
>> I'm not sure what you mean - do you mean would I be willing to write the
>> support myself?
>
> i think he's asking what the use of it is.  number 2 under the link he
> gave.

Oh, ok. Well, I'll try to summarize:

Class constants add the ability to provide scoped, "namespaced" constants.
Although one can handle basic scoping with global constants, by placing them
in interface vs implementation, they were not designed to be scope-limited.
They also do not provide a way to group several constants under a common
name, though one can simulate this functionality using creative constant
names.

Some of the benefits of class constants are: 1. They provide object-oriented
constants, 2. They simplify coding by saving the developer from having to
write long constant names to distinguish global constants from each other.
3. They reduce coding errors due to the ability to only make public what is
necessary, keeping what is visible to outside units/classes to a mimimum.

I'm sure that there are other benefits, I just can't think of them off the
top of my head.

Personally, the main reason I need them is for compatibility. I have a large
Delphi codebase that I'm porting to FPC and it uses class constants all over
the code.

Here is an example of their use:

-- Start of Unit1.pas --
unit Unit1;

interface

type
  TTest = class
    private
      const PrivateConst = '1';
    public
      const PublicConst = '1';

      constructor Create;
  end;

implementation

constructor TTest.Create;
var
  Str: String;
begin
  Str := PrivateConst;
  Str := PublicConst;
end;

end.
-- End of Unit1.pas --

-- Start of Unit2.pas --
unit Unit2;

interface

type
  TTest2 = class
      constructor Create;
  end;

implementation

uses
  Unit1;

constructor TTest.Create;
var
  Str: String;
begin
  Str := TTest.PrivateConst; // Will cause compiler error because
PrivateConst is not visible to this class.
  Str := TTest.PublicConst;
end;

end.
-- End of Unit2.pas --

Jon

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Marco van de Voort
> Henry Vermaak wrote:
> >> I'm not sure what you mean - do you mean would I be willing to write the
> >> support myself?
> >
> > i think he's asking what the use of it is.  number 2 under the link he
> > gave.
>
> Oh, ok. Well, I'll try to summarize:
>
> Class constants add the ability to provide scoped, "namespaced" constants.

A constant is already in a scope, the unit.

> Although one can handle basic scoping with global constants, by placing them
> in interface vs implementation, they were not designed to be scope-limited.
> They also do not provide a way to group several constants under a common
> name, though one can simulate this functionality using creative constant
> names.

Stuff them in one unit. unitname.constant.
 
> Some of the benefits of class constants are: 1. They provide object-oriented
> constants,

That's a description, not a benefit. Taste matters are difficult to measure.

> 2. They simplify coding by saving the developer from having to
> write long constant names to distinguish global constants from each other.
> 3. They reduce coding errors due to the ability to only make public what is
> necessary, keeping what is visible to outside units/classes to a mimimum.

Units/move them to implementation. The chance that random classes in the
same unit clash is negliable.

> Personally, the main reason I need them is for compatibility. I have a large
> Delphi codebase that I'm porting to FPC and it uses class constants all over
> the code.

IMHO that is the only decent reason. Technically IMHO it doesnt make much
sense, it's just a dotnetism, copying blindly from a different world to make
nice feature butlletlists.

And like the rest of the dotnetims, they are relatively little used,
probably mostly out D7- compat reasons. And thus they are waiting for an
implementor. I'm not aware of work being done on any of them (though I have
a vaguely recollection that the strict private and protected exist, but I'm
not sure if in fully compat form).
 
Besides implementing, making a good study of what they _EXACTLY_ do, and a
bunch of testcases/unittests could really further the cause also.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Jonas Maebe-2

On 24 Jan 2008, at 19:45, Marco van de Voort wrote:

>> Henry Vermaak wrote:
>>>> I'm not sure what you mean - do you mean would I be willing to  
>>>> write the
>>>> support myself?
>>>
>>> i think he's asking what the use of it is.  number 2 under the  
>>> link he
>>> gave.
>>
>> Oh, ok. Well, I'll try to summarize:
>>
>> Class constants add the ability to provide scoped, "namespaced"  
>> constants.
>
> A constant is already in a scope, the unit.

So are constants local to a function. Being able to restrict the  
scope to a certain function or group of functions is nevertheless  
useful.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Marco van de Voort
> On 24 Jan 2008, at 19:45, Marco van de Voort wrote:
> >> constants.
> >
> > A constant is already in a scope, the unit.
>
> So are constants local to a function. Being able to restrict the  
> scope to a certain function or group of functions is nevertheless  
> useful.

To be honest, if we wouldn't be able to do that now, and a request for
implementation came, my answer would be pretty much the same.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Jonas Maebe-2

On 24 Jan 2008, at 20:27, Marco van de Voort wrote:

>> On 24 Jan 2008, at 19:45, Marco van de Voort wrote:
>>
>>
>>> A constant is already in a scope, the unit.
>>
>> So are constants local to a function. Being able to restrict the
>> scope to a certain function or group of functions is nevertheless
>> useful.
>
> To be honest, if we wouldn't be able to do that now, and a request for
> implementation came, my answer would be pretty much the same.

I strongly disagree with that answer to those points.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

fpc book :)

Anthony Henry
I don't mean to step on Jeff Duntemann's toes but it
looks like he is going to release that book.

Read his blog entry for Jan 24th.

http://www.duntemann.com/Diary.htm


Anthony





_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpc book :)

Anthony Henry

On Thu, 2008-01-24 at 15:11 -0500, Anthony W.Henry wrote:
> I don't mean to step on Jeff Duntemann's toes but it
> looks like he is going to release that book.
>
> Read his blog entry for Jan 24th.
>
> http://www.duntemann.com/Diary.htm
>
>
> Anthony

   Suggestion: Perhaps this is worthy of a News item
on the FreePascal Web Page.

Anthony


> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fpc book :)

Felipe Monteiro de Carvalho
On Jan 24, 2008 9:26 PM, Anthony W.Henry <[hidden email]> wrote:
>    Suggestion: Perhaps this is worthy of a News item
> on the FreePascal Web Page.

Better wait until it's ready.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: class constants

Marc Weustink
In reply to this post by Marco van de Voort
Marco van de Voort wrote:

>> Marco van de Voort wrote:
>>>> Does FPC support class constants, and if it doesn't, are there any plans
>>>> to add this support?
>>> Have a look at
>>> http://www.freepascal.org/faq.var#extensionselect
>>>  
>>> The question would be "what would make this possible?"
>> What about class vars?
>
> Same answer.

Some form of classvars would make it possible to do something I do in
1) the Lazarus LCLComponent to set a class variable. (now I'm forced to
do that on every instance of that class or a derived class).
2) Store debugger configuration like is done for lazarus debuggers.
(now I store a seprate config instance in a stringlist based on the
debuggerclassname)
3) there was another case, but I don't recall it atm.

Anyway I worked my way around it, but classvars would have made this
easier. I do realize that providing patches is the best way to get
something implemented, but since my workarounds, there is no big need
anymore.

Marc

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
12