Feature #2957
Classes should support an 'extends' functionality
| Status: | Accepted | Start date: | 12/18/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 0% |
|
| Category: | language | |||
| Target version: | 2.7.x | |||
| Affected Puppet version: | 0.25.1 | Branch: | ||
| Keywords: | ||||
| Votes: | 0 |
Description
Current inheritance behaviour doesn’t cover enough functionality. It’s quite often that people want to override things other than resources in a subclass, but it doesn’t work. E.g.:
class base {
$var = foo
file { "/m": content => $var }
}
class sub inherits base {
$var = other
}
This doesn’t work for two reasons: First, because the resource is created with the base class’s value before the sub class sets its variable. This will be fixed in #2596. Second, the base and subclass have separate scopes, so the resource defined in the base class knows absolutely nothing about the subclass’s variable.
I propose we add a new kind of class relationship, called ‘extension’. It’s very similar to inheritance (and likely will entirely replace it at some point), except that the entire class hierarchy shares a single scope. Thus, in the above code, the subclass’s $var setting would actually replace the base class’s setting.
This behaviour would require that all things that affect scope — variable setting, defaults, and maybe more — would need to retain source information, like resource parameters do, so we could meet the same inheritance rules we have now. For instance, the following should be an error:
class base {
$var = foo
file { "/m": content => $var }
}
class sub1 extends base {
$var = other
}
class sub2 extends base {
$var = yay
}
include sub1, sub2
But this would not:
class base {
$var = foo
file { "/m": content => $var }
}
class sub1 extends base {
$var = other
}
class sub2 extends sub1 {
$var = yay
}
include sub1, sub2
This works because sub2 is a child of sub1, rather than a peer, so it can override its parent.
It’s currently unresolved whether extension and inheritance can be used in the same class hierarchy. Instinctively, it seems like they could, but I think that would be pretty confusing.
Related issues
History
Updated by Luke Kanies almost 2 years ago
- Target version changed from 2.6.0 to 2.7.x
No way this is happening in rowlf.