Sign in Join

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

Mailing lists / fpc-devel / 2019-03

Re: Typed constants

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

J. Gareth Moreton

I've actually had this problem myself - sometimes it's quite critical as well because you might want things like

 const
   Pi: Double = 3.14159265359;
   HalfPi: Double = Pi / 2;  TwoPi: Double = 2 * Pi;

Will return the illegal expression error as well, even if Pi is fully declared elsewhere, and the only fix was to expand the Pi identifier into the number it represents.  This might be a problem with "pure" functions down the line as well.
Gareth aka. Kit

On Sun 17/03/19 23:50 , "Ryan Joseph" sent:
I just realized that typed constants exist and may need to be supported as constants for generics but I’ve never used them myself (not sure when they were introduced).

First test gives an error so it makes me wonder if they’re buggy and should be allowed for generics. Should they?

 const
 kInt: integer = 100;
 var
 i: integer = kInt; // ERROR: Illegal expression

They don’t work for generics right now either because they appear as a load node and are not discernible from a variable. Not even sure how to know if a load node is a typed const or what these things really are.

Regards,
Ryan Joseph

Reply by email

Ryan Joseph

I just realized that typed constants exist and may need to be supported as constants for generics but I’ve never used them myself (not sure when they were introduced).

First test gives an error so it makes me wonder if they’re buggy and should be allowed for generics. Should they?

const
  kInt: integer = 100;
var
  i: integer = kInt;	// ERROR: Illegal expression

They don’t work for generics right now either because they appear as a load node and are not discernible from a variable. Not even sure how to know if a load node is a typed const or what these things really are.

Regards,
Ryan Joseph

Reply by email

Sven Barth
to Ryan Joseph

Ryan Joseph schrieb am Mo., 18. März 2019,
00:50:

Show quoted text 16 lines
I just realized that typed constants exist and may need to be supported as constants for generics but I’ve never used them myself (not sure when they were introduced). First test gives an error so it makes me wonder if they’re buggy and should be allowed for generics. Should they? const kInt: integer = 100; var i: integer = kInt; // ERROR: Illegal expression They don’t work for generics right now either because they appear as a load node and are not discernible from a variable. Not even sure how to know if a load node is a typed const or what these things really are.

This is by design. Typed constants are essentially read only variables (in TP times they were writable by default, nowadays that can be controlled with $J) and thus they behave differently to untyped constants and can't be used as initializers for other constants or variables or as default parameters.
That's also why I said to restrict generic const parameters to those for which a const node exists, cause those are true constants. So as for now you definitely don't need to support typed constants, they need to be rejected. We can always add support later, but we should first get a feel how generics with constants behave and how they're used before we open that can of worms.

Regards,
Sven

Reply by email