Parameterless constructors are not allowed in records or record/type helpers

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

Parameterless constructors are not allowed in records or record/type helpers

Ryan Joseph
Why is this restriction in place and what are the alternatives if any? Some times I want to just provide a method that sets default values for a record without any parameters but FPC doesn’t let me for some reason.

Regards,
        Ryan Joseph

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

Re: Parameterless constructors are not allowed in records or record/type helpers

Sven Barth-2

Am 05.02.2017 09:13 schrieb "Ryan Joseph" <[hidden email]>:
>
> Why is this restriction in place and what are the alternatives if any? Some times I want to just provide a method that sets default values for a record without any parameters but FPC doesn’t let me for some reason.

Delphi compatibility.
As a workaround you could add a constructor with a default parameter.

Regards,
Sven


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

Re: Parameterless constructors are not allowed in records or record/type helpers

Ryan Joseph

> On Feb 5, 2017, at 3:27 PM, Sven Barth <[hidden email]> wrote:
>
> constructor with a default parameter.

How? never heard of this. Thanks.

Regards,
        Ryan Joseph

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

Re: Parameterless constructors are not allowed in records or record/type helpers

Sven Barth-2

Am 05.02.2017 09:39 schrieb "Ryan Joseph" <[hidden email]>:
>
>
> > On Feb 5, 2017, at 3:27 PM, Sven Barth <[hidden email]> wrote:
> >
> > constructor with a default parameter.
>
> How? never heard of this. Thanks.

See here at the bottom: http://freepascal.org/docs-html/current/ref/refsu65.html#x176-19800014.4.1 (it's the same for any kind of routine: function, procedure, method, constructor...)

Regards,
Sven


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

Re: Parameterless constructors are not allowed in records or record/type helpers

Ryan Joseph
I never knew Pascal had default parameters, a feature I always use and enjoy in PHP. Once again I learn something new about the language I use. :)

However, the constructor with default params does not work. I get the same error as before. {$modeswitch advancedrecords} is on.

type
        MyRecord = record
                x: integer;
                constructor Foo (_x: integer = 0);
        end;


> On Feb 5, 2017, at 8:29 PM, Sven Barth <[hidden email]> wrote:
>
> See here at the bottom: http://freepascal.org/docs-html/current/ref/refsu65.html#x176-19800014.4.1 (it's the same for any kind of routine: function, procedure, method, constructor...)

Regards,
        Ryan Joseph

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

Re: Parameterless constructors are not allowed in records or record/type helpers

Maciej Izak

2017-02-05 14:34 GMT+01:00 Ryan Joseph <[hidden email]>:
However, the constructor with default params does not work. I get the same error as before. {$modeswitch advancedrecords} is on.

type
        MyRecord = record
                x: integer;
                constructor Foo (_x: integer = 0);
        end;

AFAIK this is by design. In theory "constructor Create;" is reserved for record "Initializers" (which means initialization for managed fields of record). Parameter less constructor doesn't have much sense for records. Use "class function Foo: MyRecord ;" instead. Probably syntax "constructor Create;" will be used for record initializers in next Delphi releases (around 2018?). In FPC we have implemented this feature (not merged yet : http://bugs.freepascal.org/view.php?id=30687 ) but as "class operator Initialize(var a: MyRecord);".

--
Best regards,
Maciej Izak

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

Re: Parameterless constructors are not allowed in records or record/type helpers

Sven Barth-2
In reply to this post by Ryan Joseph

Am 05.02.2017 15:04 schrieb "Ryan Joseph" <[hidden email]>:
>
> I never knew Pascal had default parameters, a feature I always use and enjoy in PHP. Once again I learn something new about the language I use. :)

I suggest you to read through the language reference guide then ;)

> However, the constructor with default params does not work. I get the same error as before. {$modeswitch advancedrecords} is on.

Oh, well, it was worth a shot... Alternatively you could declare a class function named Create that returns an instance of the record. Even Delphi is using that for Rtti.TRttiContext.

Regards,
Sven


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

Re: Parameterless constructors are not allowed in records or record/type helpers

Ryan Joseph
I tried class functions in records already but I got the error "Class methods must be static in records”. Not sure what that means but I assumed they don’t work either.

Too bad, that would be a nice feature.

> On Feb 6, 2017, at 2:29 AM, Sven Barth <[hidden email]> wrote:
>
> Oh, well, it was worth a shot... Alternatively you could declare a class function named Create that returns an instance of the record. Even Delphi is using that for Rtti.TRttiContext.

Regards,
        Ryan Joseph

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

Re: Parameterless constructors are not allowed in records or record/type helpers

Sven Barth-2

Am 06.02.2017 02:37 schrieb "Ryan Joseph" <[hidden email]>:
>
> I tried class functions in records already but I got the error "Class methods must be static in records”. Not sure what that means but I assumed they don’t work either.

You need to add the "static" modifier to the method. Class functions in records can't have a self parameter and the "static" disables that. That's why.

Regards,
Sven


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

Re: Parameterless constructors are not allowed in records or record/type helpers

Ryan Joseph
Thanks got it now. Not ideal but it works and is a small improvement over a function.

> On Feb 6, 2017, at 2:06 PM, Sven Barth <[hidden email]> wrote:
>
> You need to add the "static" modifier to the method. Class functions in records can't have a self parameter and the "static" disables that. That's why.
>
>

Regards,
        Ryan Joseph

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