Sign in Join

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

Mailing lists / fpc-devel / 2005-05

No default property value allowed for double type?

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Bram Kuijvenhoven

Hi,

Is there a reason why one can't specify a default value for a property of type double, or is this a bug? (In the latter case I'll file it as a bug)

Compiler version: 2.0.0, Win32
OS: Win XP (though I guess that is irrelevant)

Example code:
===========
program DoublePropDefault;

type
  TMyClass = class
  private
    FA: double;
  published
    property A:double read FA write FA default 100;
  end;
begin
end.
===========

Compiler error:
===========
DoublePropDefault.pas(10,48) Error: Property can't have a default value ===========

Note: this code does compile when replacing 'double' for for example 'single'.

Bye,

Bram Kuijvenhoven

Reply by email

Peter Vreman
to Bram Kuijvenhoven
Show quoted text 31 lines
Hi, Is there a reason why one can't specify a default value for a property of type double, or is this a bug? (In the latter case I'll file it as a bug) Compiler version: 2.0.0, Win32 OS: Win XP (though I guess that is irrelevant) Example code: =========== program DoublePropDefault; type TMyClass = class private FA: double; published property A:double read FA write FA default 100; end; begin end. =========== Compiler error: =========== DoublePropDefault.pas(10,48) Error: Property can't have a default value =========== Note: this code does compile when replacing 'double' for for example 'single'.

I don't know the reason why single is allowed. Delphi doesn't allow default values for single or double.

Reply by email

Bram Kuijvenhoven
to Peter Vreman
Peter Vreman wrote
I don't know the reason why single is allowed. Delphi doesn't allow default values for single or double.

I looked it up in the Delphi documentation (I should have done that before) and found out the default directive even has a different meaning from what I thought :)

I thought it would set a default value for that property, but that isn't true; you still need to do that in the constructor. I will provide a small explanation for reference.

default is (for non-array properties) a so called storage specifier. Published component properties with a default value are only written to a form file during saving of the component when their value differs from the specified default value. The Delphi documentation says that one can only specify a default value for properties of
- an ordinal type or
- a set type with the lower and upper bound of the ordinal values of the sets base type in the range 0..31 Reals, strings, and pointers have the implicit default values 0, '' and nil respectively. Furthermore, there exists an stored directive which controls whether the property is written at all and a nodefault specifier which simply indicates the absence of a default value. By default a property has no default and is stored. The stored directive has a boolean constant, a boolean field, or a parameterless method returning a boolean as parameter.

Note that the default directive for array properties indicates it is the default (array) property for that class (and is parameterless), allowing syntaxis like

type
TList = class
...

    property Items[i:integer]:pointer read GetItem write SetItem default;
    ...
  end;

...
var

  MyList:TList;

...
MyList[i]

as explained in the FPC docs.

The storage specifiers are not explained in the FPC reference manual as far as I know, though they are in the syntaxis diagram of course.

And somehow FPC allows default values for singles.

Bye,

Bram

Reply by email

Florian Klaempfl
to Bram Kuijvenhoven
Bram Kuijvenhoven wrote
The storage specifiers are not explained in the FPC reference manual as far as I know, though they are in the syntaxis diagram of course.

Michael?

And somehow FPC allows default values for singles.

... because they have a size of 4 which is required for a default value :)

Reply by email