Swift-模板参数

发布于 2025-02-09 05:06:30 字数 551 浏览 1 评论 0原文

我想知道是否有一种方法可以执行相当于c ++的模板,以定义某些整数上的通用类型基础。我知道Swift的通用类型,但我不想提供一种类型,而是INT。

在C ++中,看起来像:

template <int N>
class Vector {
  int values[N];
}

Swift中,我想使用它,就像

let vector = Vector<3>(1, 2, 4)
print(vector.sum())

Swift提供了一种基于类型但不是INT的模板的方法,这看起来很奇怪。我几乎可以使用generics和sizeof(t),通过intfloatdouble,但是'D很棒。

I'm wondering if there is a way to do the equivalent of C++'s templating, to define a generic type base on some integer. I know of Swift's Generic Type, but I don't want to provide a Type but an Int.

In C++, it would look like :

template <int N>
class Vector {
  int values[N];
}

And in Swift, I'd like to use it like

let vector = Vector<3>(1, 2, 4)
print(vector.sum())

It seems weird that Swift provides a way to template based on Type but not on int. I could almost achieve it using generics and sizeof(T), passing Int, Float or Double, but that'd be aweful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2025-02-16 05:06:30

不幸的是,Swift不支持这一点,因此我最终使用了一些协议来定义整数值。


protocol SizeableClass {
    static func size() -> Int
}

struct One : SizeableClass {
    static func size() -> Int { 1 }
}
struct Two : SizeableClass {
    static func size() -> Int { 2 }
}
struct Three : SizeableClass {
    static func size() -> Int { 3 }
}


class Vector<S: SizeableClass, T: SignedNumeric> {
    let size = S.size()
    var values: Array<T>
    
    init(_ values: [T]) {
        self.values = values
    }
    
    static func +(_ first: Vector<S, T>, _ second: Vector<S, T>) -> Vector<S, T> {
        return Vector(
            zip(first.values, second.values).map {
                $0 + $1
            }
        )
    }
    
    static prefix func -(_ vector: Vector<S, T>) -> Vector<S, T> {
        return Vector(vector.values.map { -$0 })
    }
    
    static func -(_ first: Vector<S, T>, _ second: Vector<S, T>) -> Vector<S, T> {
        return first + -second
    }
    
    static func *(_ lambda: T, _ vector: Vector<S, T>) -> Vector<S, T> {
        return Vector(vector.values.map {
            $0 * lambda
        })
    }
}

类型检查非常有效(无法添加vector&lt;三,int&gt;,带有vector&lt;二,int int&gt;),尽管写作有点重。
我愿意接受建议!

It's unfortunate that Swift doesn't support this, so I ended up using some protocol to define an integer value.


protocol SizeableClass {
    static func size() -> Int
}

struct One : SizeableClass {
    static func size() -> Int { 1 }
}
struct Two : SizeableClass {
    static func size() -> Int { 2 }
}
struct Three : SizeableClass {
    static func size() -> Int { 3 }
}


class Vector<S: SizeableClass, T: SignedNumeric> {
    let size = S.size()
    var values: Array<T>
    
    init(_ values: [T]) {
        self.values = values
    }
    
    static func +(_ first: Vector<S, T>, _ second: Vector<S, T>) -> Vector<S, T> {
        return Vector(
            zip(first.values, second.values).map {
                $0 + $1
            }
        )
    }
    
    static prefix func -(_ vector: Vector<S, T>) -> Vector<S, T> {
        return Vector(vector.values.map { -$0 })
    }
    
    static func -(_ first: Vector<S, T>, _ second: Vector<S, T>) -> Vector<S, T> {
        return first + -second
    }
    
    static func *(_ lambda: T, _ vector: Vector<S, T>) -> Vector<S, T> {
        return Vector(vector.values.map {
            $0 * lambda
        })
    }
}

Type checking works great (can't add Vector<Three, Int> with Vector<Two, Int> for example), though it's a bit heavy to write.
I'm open to suggestions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文