Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JacobFV opened this issue May 25, 2022 · 0 comments
Labels

Comments

@JacobFV
Copy link

JacobFV commented May 25, 2022

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.
@evhub evhub added the feature label May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants