Skip to content

String, Arrays and Ranges should be more symmetric to each other #30

@manuel-fischer

Description

@manuel-fischer

In many languages strings are handled exactly like arrays. In TScript there are some asymmetries between arrays and strings:

  1. Strings are not iterable in for loops. Each character should be iterated.
  2. Strings are concatenated with +, arrays with concat, (compare Python, Java, etc. which use + for both). Later dialects of BASIC, that allowed string concatenation, also provided the & operator in addition to the + operator. It might be a good idea for beginners to differenciate string concatenation from addition, by introducing the & operator for string and sequence concatenation.
  3. Arrays have no function for finding elements/sequences in them. There could be a function array.find(subArray), that works exactly like the find function on strings. When you want to search for a single element, you would simply call array.find([element]) (Square brackets denote the creation of an array here). It might be appropriate to use different function names like array.findSequence(subArray) and array.findElement(element) to disambiguate the function name.
  4. Strings and ranges have no keys() and no values() function

As long as some these asymmetries exist, you can use a string like an array, by simply converting it to an array. This would be done with var chars = string.split("");. So to iterate through a string, the following could be used:

# instead of
#   for var char in string do ...
# you can currently only use:
for var char in string.split("") do ...
# or
for var i in 0:string.size() do
{
    var char = string[i];
    ...
}

But this might be unintuitive for beginners.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions