coconut icon indicating copy to clipboard operation
coconut copied to clipboard

Feature: Use name of meta/superclass instead of writing `class <name> (<meta/superclass name>)`

Open JacobFV opened this issue 2 years ago • 0 comments

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:

# 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
    meta MakeSerializable: ...
    # same as
    class MakeSerializable (type): ...
    
    1. abstract: declares that a given class is a subclass of abc.ABC
    abstract MyAbstractClass: ...
    # same as
    class MyAbstractClass (abc.ABC): ...
    
    1. static: annotates all methods in the given class as staticmethods
    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): ...
    
    1. subclass: makes the given class a subclass of its containing class; the subclass declaration must take place inside another class.
    class Super:
        subclass Sub1: ...
        subclass Sub2: ...
    # same as
    class Super:
        class Sub1(Super): ...
        class Sub2(Super): ...
    
    1. private: you get the idea
  2. rule blocks for reactive subgraph rewriting
    rule find_hydroxyl_group ({'R': Node, 'O': Node('O'), 'H': Node('H')}):
        return Hydroxyl(R)
    
  3. reactive programming constructs (don't ask how):
    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.

JacobFV avatar May 25 '22 21:05 JacobFV