Feature: Use name of meta/superclass instead of writing `class <name> (<meta/superclass name>)`
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 142
- PR merge metrics
- No merged PRs in 30d
Description
Sometimes you have a bunch of small classes that don't justify all the trouble of writing `class` a hundred times. The same could be said for subclasses or a class full of static methods. **Can coconut provide the option to use a meta/superclass' name instead of the `class` keyword?**
Such a feature would be used like so:
```python
# CST for the CST
data Node(name: str): ...
Node Concat(operands: Node[]): ...
Node Union(operands: Node[]): ...
Node Disj(generator: Node, discriminator: Node): ...
# CST
Noun = Union(load_nouns(...))
Verb = Union(load_verbs(...))
...
Sentence = SVO | OVS | SOV
# generate AST
sentence = Sentence.generate(...)
```
Custom keyword'ed blocks would:
1. draw more attention to the actual data structure being used, thus helping the developer think in terms of their own program's structure, rather than strictly thinking in terms of the Python/Coconut semantics
2. reduce repetitive keywords and improve code readability
3. be convenient
Besides writing generic `superclass_name subclass_name: ...` statements, other use-cases would be:
1. OOP keywords:
1. `meta/metaclass`: declares that a given class is a subclass of `type`
```python
meta MakeSerializable: ...
# same as
class MakeSerializable (type): ...
```
3. `abstract`: declares that a given class is a subclass of `abc.ABC`
```python
abstract MyAbstractClass: ...
# same as
class MyAbstractClass (abc.ABC): ...
```
4. `static`: annotates all methods in the given class as `staticmethods`
```python
abstract MyStaticClass:
_a = 1
def foo(a, b): ...
def bar(): ...
def baz(a): ...
# same as
class MyStaticClass: ...
_a = 1
@staticmethod
def foo(a, b): ...
@staticmethod
def bar(): ...
@staticmethod
def baz(a): ...
```
5. `subclass`: makes the given class a subclass of its containing class; the `subclass` declaration must take place inside another class.
```python
class Super:
subclass Sub1: ...
subclass Sub2: ...
# same as
class Super:
class Sub1(Super): ...
class Sub2(Super): ...
```
7. `private`: you get the idea
2. `rule` blocks for reactive subgraph rewriting
```python
rule find_hydroxyl_group ({'R': Node, 'O': Node('O'), 'H': Node('H')}):
return Hydroxyl(R)
```
3. reactive programming constructs (don't ask how):
```python
when x>5:
x=0
```
Alternatives:
- Decorators accomplish the same functionality, but they are not as concise, especially when you have to write hundreds of rules for your biochemistry simulator.
- Directly specify the metaclass. Metaclasses are not as easy to read as when they are buried around subclasses and metaclass kwargs.
- Write a domain specific language. DSL's sacrifice flexibility for speed and clarity. However if the recommended feature is accepted into coconut, developers can have both.
Contributor guide
Assessment
This issue has not been assessed yet.