Bug #10506
Naming rules are inconsistent
| Status: | Accepted | Start date: | 11/03/2011 | |
|---|---|---|---|---|
| Priority: | Urgent | Due date: | ||
| Assignee: | % Done: | 0% | ||
| Category: | language | |||
| Target version: | 3.x | |||
| Affected Puppet version: | Branch: | |||
| Keywords: | usability syntax | |||
Description
The rules for naming of classes is inconsistently applied to different language concepts.
- It is allowed to name a class using NUMBER (e.g. ‘0’, ‘0.0’, ‘0477’, ‘0x1f’, ‘1.3e-2’).
- A dollar variable is parsed with the regular expression
%r{\$(::)?([-\w]+::)*[-\w]+}and can thus never refer to a variable in a class named with a ‘.’ (period) in the name. (Update: A hyphen in the variable is no longer allowed, in 3.x it is gone in the regexp, and in 2.7.20? it is possible to turn it on, but also getting a deprecation warning). - A NAME, CLASSREF, or CLASSNAME is parsed with
%r{((::)?[a-z0-9][-\w]*)(::[a-z0-9][-\w]*)*}and can also not contain a ‘.’ (period). Update, now that hyphens in variables are not allowed, it should also be disallowed in names. - Regular expression matching injects values into variables
$0–$n(although not directly clashing since a class is typically not referenced with a variable, it is still confusing to see say$0::1, when $0 is a regexp match) - Class defaults can be set using a class reference (each segment starts with upper case letter in a fully qualified name), this is strange when name is numeric (how do you write an upper case ‘1’?). Compare:
using words:
class one($a) { class two($b) {} }
One { a=>10 }
One::Two {b=>20}
using numbers:
class 1($a) { class 2($b) {} }
1 { a=>10 }
1::2 {b=>20}
The second example (naturally) results in:
Could not parse for environment production: All resource specifications require names at /private/tmp/test.pp:2 on node xxx
- A separate issue is that assignment to
$0–$nmay not take effect (the variable is still undefined) in some cases. - The documentation does not seem to mention the fact that classes may have numerical names (the stricter rules are shown in several places).
I suggest that:
* Numerical names should be deprecated
* Assignment to $0 – $n should be illegal (reserved for regular expression matches)
In contrast, if trying to fix this by adding ‘.’ (period) as valid in variables, a lot of string interpolation will break (in similar ways to how the altered string interpolation rules regarding ‘–’ (hyphen) breaks existing code).
The interpolation issues could be resolved by deprecating the interpolation short form (i.e. interpolating without braces), but that may be considered a more serious imposition (even if recommended as best practice) – i.e. that "Hello $name" must be written "Hello ${name}".
It seems like 2.8 would be a good candidate for more stringent rules since dynamic scoping of variables will be deprecated and people do have to inspect their code and deal with naming/reference issues.
Related issues
History
#1
Updated by James Turnbull over 1 year ago
- Status changed from Unreviewed to Needs Decision
- Assignee set to Nigel Kersten
#2
Updated by Nigel Kersten over 1 year ago
- Status changed from Needs Decision to Accepted
- Assignee deleted (
Nigel Kersten) - Priority changed from Normal to Urgent
- Target version set to 3.x
#3
Updated by eric sorenson 11 months ago
- Assignee set to eric sorenson
Taking ownership of this as part of #5268 — Henrik, thank you for the analysis.
#4
Updated by Henrik Lindberg 8 months ago
- Description updated (diff)
Updated description now that hyphens in variables are deprecated in 2.7.x and not allowed in 3.x.
#5
Updated by Erik Dalén 3 months ago
Oddly you are allowed to name a class “class”, but then you can only include it if you quote it before passing to include function. According to documentation it should not be allowed to name a class to a reserved word such as “class”.