support creation of list via multiplication operator #48

Closed
opened 2021-10-03 17:19:52 +08:00 by sb10q · 10 comments
def run() -> int32:
    list_a = [1]*100
    return 0

currently fails with

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Cannot unify record[__mul__=call] with list[int32] at line 2 column 17"', nac3standalone/src/main.rs:65:35
```python def run() -> int32: list_a = [1]*100 return 0 ``` currently fails with ``` thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Cannot unify record[__mul__=call] with list[int32] at line 2 column 17"', nac3standalone/src/main.rs:65:35 ```

This requires modification of the type system. The current implementation separates list and objects, so lists cannot have methods. Maybe we would need to treat lists as objects.

This requires modification of the type system. The current implementation separates list and objects, so lists cannot have methods. Maybe we would need to treat lists as objects.
Poster
Owner

This is a very common way of creating large lists (is there any other way?) so we definitely need to support that.

This is a very common way of creating large lists (is there any other way?) so we definitely need to support that.
sb10q added the
high-priority
label 2021-10-03 17:58:18 +08:00

Two ways to do this. We can either support this via modifying the type checking module (would break quite a lot of tests and other code...) or use a special builtin function to do it. The second option would be much simpler to implement for now.

Maybe something like this:

# a = [1] * 100
a = repeat([1], 100)

What do you think?

Two ways to do this. We can either support this via modifying the type checking module (would break quite a lot of tests and other code...) or use a special builtin function to do it. The second option would be much simpler to implement for now. Maybe something like this: ```python # a = [1] * 100 a = repeat([1], 100) ``` What do you think?
Poster
Owner

The repeat function is fine but it should be called something more specific. Maybe new_list or similar. It could be simply called as new_list(1, 100) without the [].

For the syntactic sugar, do an AST match-pattern-and-replace pass before typechecking? That pass could also handle things like int64(round()) -> round64().

The ``repeat`` function is fine but it should be called something more specific. Maybe ``new_list`` or similar. It could be simply called as ``new_list(1, 100)`` without the ``[]``. For the syntactic sugar, do an AST match-pattern-and-replace pass before typechecking? That pass could also handle things like ``int64(round())`` -> ``round64()``.

The repeat function is fine but it should be called something more specific. Maybe new_list or similar. It could be simply called as new_list(1, 100) without the [].

new_list(T, int) -> [T] would not support things like [1, 2, 3] * 100, although I'm not sure if users want this feature.

For the syntactic sugar, do an AST match-pattern-and-replace pass before typechecking? That pass could also handle things like int64(round()) -> round64().

For syntactic sugar, I think the best we can do is just support thing like [...] * .... Things more complicated like

a = [1]
b = a * 100

is not supported unless we modify the type system.

> The ``repeat`` function is fine but it should be called something more specific. Maybe ``new_list`` or similar. It could be simply called as ``new_list(1, 100)`` without the ``[]``. `new_list(T, int) -> [T]` would not support things like `[1, 2, 3] * 100`, although I'm not sure if users want this feature. > For the syntactic sugar, do an AST match-pattern-and-replace pass before typechecking? That pass could also handle things like ``int64(round())`` -> ``round64()``. For syntactic sugar, I think the best we can do is just support thing like `[...] * ...`. Things more complicated like ``` a = [1] b = a * 100 ``` is not supported unless we modify the type system.
Poster
Owner

Those restrictions are fine IMHO.

Those restrictions are fine IMHO.

suddenly remember that the user should be able to use [a for _ in range(x)]. Do we still need the workaround for now?

suddenly remember that the user should be able to use `[a for _ in range(x)]`. Do we still need the workaround for now?
Poster
Owner

I suppose only range(int32) is to be supported as iterator there.
Can this be implemented quickly and without runtime performance issues?

I suppose only ``range(int32)`` is to be supported as iterator there. Can this be implemented quickly and without runtime performance issues?

Can this be implemented quickly

This may need some time... I'm thinking about how to deal with range without a range type for now.

without runtime performance issues?

I think we can optimize the implementation to make it fast.

> Can this be implemented quickly This may need some time... I'm thinking about how to deal with `range` without a range type for now. > without runtime performance issues? I think we can optimize the implementation to make it fast.

Implemented with list comprehension.

Implemented with list comprehension.
Sign in to join this conversation.
No Milestone
No Assignees
2 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#48
There is no content yet.