classes without __init__ should be valid #221

Closed
opened 2022-03-17 21:30:42 +08:00 by sb10q · 4 comments
@extern
def output_int32(x: int32):
    ...

class A:
    def foo(self):
        output_int32(1)


def run() -> int32:
    inst = A()
    inst.foo()
    return 0

> ./run_demo.sh src/classes.py
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "type error at identifier `A` (cannot get type of A) at src/classes.py: line 11 column 12"', nac3standalone/src/main.rs:223:35
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

expected output: 1

```python @extern def output_int32(x: int32): ... class A: def foo(self): output_int32(1) def run() -> int32: inst = A() inst.foo() return 0 ``` ``` > ./run_demo.sh src/classes.py thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "type error at identifier `A` (cannot get type of A) at src/classes.py: line 11 column 12"', nac3standalone/src/main.rs:223:35 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` expected output: 1
sb10q added this to the Alpha milestone 2022-03-28 22:13:11 +08:00
wylited was assigned by sb10q 2022-04-07 13:55:01 +08:00

Actually we won't be able to differentiate between the case which the constructor exists but does not have @kernel/@portable from the case which the constructor does not exist at all. This is due to how we handle kernel/portable declaration (filtering in nac3artiq).

Actually we won't be able to differentiate between the case which the constructor exists but does not have `@kernel`/`@portable` from the case which the constructor does not exist at all. This is due to how we handle kernel/portable declaration (filtering in nac3artiq).
Poster
Owner

The solution seems pretty straightforward to me: add a boolean flag in nac3core that determines whether to accept creation of classes without an explicit __init__.

nac3standalone would set the flag and creation of such classes would succeed.

nac3artiq would not set it, and the user needs to define __init__ and decorate it with @kernel or @portable for nac3core to see it and allow the creation of the class.

We need to do the right thing if the user mistakenly decorates __init__ with @rpc.

The solution seems pretty straightforward to me: add a boolean flag in nac3core that determines whether to accept creation of classes without an explicit ``__init__``. nac3standalone would set the flag and creation of such classes would succeed. nac3artiq would not set it, and the user needs to define ``__init__`` and decorate it with ``@kernel`` or ``@portable`` for nac3core to see it and allow the creation of the class. We need to do the right thing if the user mistakenly decorates ``__init__`` with ``@rpc``.

what is "the right thing"? throwing an error?

what is "the right thing"? throwing an error?
Poster
Owner

Yes, with a user-friendly message.

Yes, with a user-friendly message.
wylited was unassigned by sb10q 2022-07-04 18:10:34 +08:00
z78078 was assigned by sb10q 2022-07-04 18:10:37 +08:00
sb10q closed this issue 2022-07-11 15:56:22 +08:00
Sign in to join this conversation.
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#221
There is no content yet.