Skip to content

Strings cannot be iterated #843

@ark120202

Description

@ark120202

Playground

Input:

for (const char of 'foo') {
    print(char);
}

Current Result:

function __TS__Iterator(iterable)
    if iterable[Symbol.iterator] then
        local iterator = iterable[Symbol.iterator](iterable)
        return function()
            local result = iterator:next()
            if not result.done then
                return result.value
            else
                return nil
            end
        end
    else
        local i = 0
        return function()
            i = i + 1
            return iterable[i]
        end
    end
end

for char in __TS__Iterator("foo") do
    print(char)
end

Expected Result:

function __TS__Iterator(iterable)
    if iterable[Symbol.iterator] then
        local iterator = iterable[Symbol.iterator](iterable)
        return function()
            local result = iterator:next()
            if not result.done then
                return result.value
            else
                return nil
            end
        end
	elseif type(iterable) == "string" then
        local i = 0
        return function()
            i = i + 1
            return string.sub(iterable, i, i)
        end
    else
        local i = 0
        return function()
            i = i + 1
            return iterable[i]
        end
    end
end

for char in __TS__Iterator("foo") do
    print(char)
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions