DavidBuchanan314 / boiga

A Python library enabling ergonomic Scratch 3.0 code generation.
MIT License
34 stars 3 forks source link

Add option to disable 0-based indexes #4

Closed MystPi closed 2 years ago

MystPi commented 2 years ago

In some projects, 0-based indexes are more trouble than they're worth. They can also generate bugs like the following pseudocode:

set [last v] to [last]
say (item (last) of [mylist v])

Which gets turned into:

set [last v] to [last]
say (item ((last) + 1) of [mylist v])

I propose an option to disable 0-based indexes. Here's an idea of how the interface might work:

project = Project(zero_indexes = False)
DavidBuchanan314 commented 2 years ago

I agree that 0-indexing is not always worth it, and I would like to add an option for 1-indexing. However, IMHO it would be confusing for python [] notation to ever mean 1-indexing, especially if it was in the same source file as 0-indexed operations on normal python lists.

I think the best way to handle this is to create special methods for 1-indexing, eg something like my_list.oneindex(index) - or perhaps my_list.item(index), to match the naming of the scratch block

DavidBuchanan314 commented 2 years ago

I've added .item() for lists and strings, along with .delete_at1() and .index1() for lists

MystPi commented 2 years ago

Thank you! That helps a lot!