← 输入框|github.com|刷新
Skip to content

For loop should be implemented for strings without declaration  #1475

Closed

Description

@faze-geek

Related to #206 which was closed couple of days ago.

a :str
for a in "abc":
      print (a)

(lp) C:\Users\kunni\lpython>python try.py
a
b
c

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
semantic error: Only function call `range(..)` supported as for loop iteration for now
 --> try.py:3:5 - 4:17
  |
3 |        for a in "abc":
  |        ^^^^^^^^^^^^^^^...
...
  |
4 |            print (a)
  | ...^^^^^^^^^^^^^^^^^

Considering range is the only supported function, a string input is not another function like reverse(str_name) or enumerate(str_name) so a better error message can be provided if this is not in the implementation plan as of now.

Activity

  1. certik commented on Jan 26, 2023

    @certik
    Contributor

    I think we should implement this. You can change the error message if you want in the meantime.

  2. faze-geek commented on Jan 27, 2023

    @faze-geek
    ContributorAuthor

    I think we should implement this. You can change the error message if you want in the meantime.

    Yes sure! I'll be attempting a few PR's once run_tests starts correctly working with windows. If I'm not wrong #1463 is a shot at that.

  3. anutosh491 commented on Feb 11, 2023

    @anutosh491
    Collaborator

    I've been trying to address this issue , but not sure what approach could be used for the same . Let's say we want to address

    i: i32
    for i in [1, 2, 3]:
         print(i)
    

    Here the iterable we have is of type AST::List_t and we currently can't address this case but we can address cases where the list has been assigned to a variable .

    i: i32
    lst: list[i32] = [1, 2, 3]
    for i in list:
          print(i)
    

    Here the iterable is of type AST::Name_t . So what comes to my mind for addressing the first case is ... that why don't we assign a dummy name to the list and create an object of AST::Name_t type and repeat the procedure what's being done already . Not sure if this is the way to go here @Thirumalai-Shaktivel !?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions