Невозможно вызвать 'stride' со списком аргументов типа '(from: T, to: T, by: T)' Swift - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь создать общую функцию c, которая будет работать как для Integer, так и для Double. Но каким-то образом я получаю ошибку Cannot invoke 'stride' with an argument list of type '(from: T, to: T, by: T)'. Ниже мой код:

func generateList<T: SignedNumeric>(from: T, to: T, step: T, addLastValue: Bool = true) -> [T] where T: Comparable & Strideable {
        var items = [T]()

        if step == 0 || from == to {
            return [from]
        }

        for i in stride(from: from, to: to, by: step) {
            items.append(i)
        }

        if addLastValue && to > items.last ?? to {
            items.append(to)
        }

        return items
    }

1 Ответ

1 голос
/ 08 мая 2020

шаг должен быть типа T.Stride

func generateList<T: SignedNumeric>(from: T, to: T, step: T.Stride, addLastValue: Bool = true) -> [T] where T: Comparable & Strideable {
    var items = [T]()

    if step == 0 || from == to {
        return [from]
    }

    for i in stride(from: from, to: to, by: step) {
        items.append(i)
    }

    if addLastValue && to > items.last ?? to {
        items.append(to)
    }

    return items
}
...