Generic type constraint for numerical type only

后端 未结 3 1980
天涯浪人
天涯浪人 2020-12-14 16:02

I\'m trying to figure out how to implement a type constraint for a generic class (in Swift) that will limit the generic types to numeric types only. For instance Double, Int

相关标签:
3条回答
  • 2020-12-14 16:05

    You can specify type constraints (using both classes and protocols) for a generic class (same syntax applies to functions) using angle brackets:

    class Foo<T: Equatable, U: Comparable> { }
    

    To specify more than one requirement on a single type, use a where clause:

    class Foo<T: UIViewController where T: UITableViewDataSource, T: UITextFieldDelegate> { }
    

    However, it doesn't look like you can specify optional requirements in a generic parameter clause, so one possible solution is to create a protocol that all the numeric types implement via extensions and then constrain your class on that requirement:

    protocol Numeric { }
    
    extension Float: Numeric {}
    extension Double: Numeric {}
    extension Int: Numeric {}
    
    
    class NumberCruncher<C1: Numeric> {
        func echo(num: C1)-> C1 {
            return num
        }
    }
    
    NumberCruncher<Int>().echo(42)
    NumberCruncher<Float>().echo(3.14)
    
    0 讨论(0)
  • 2020-12-14 16:29

    Strideable is the smallest standard protocol that every standard number type conforms to, but it also has a few more types conforming to it. http://swiftdoc.org/protocol/Strideable/hierarchy/

    Or you can use IntegerType and FloatingPointType.

    0 讨论(0)
  • 2020-12-14 16:30

    As talked about here and here, the Numeric protocol is now part of Swift 4, so - for example - you can overload + for numeric tuples as follows:

    func +<T : Numeric> (x: (T, T), y: (T, T)) -> (T, T) { 
         return (x.0 + y.0, x.1 + y.1) 
    }
    
    (1,1) + (2,3) // (3, 4) : (Int, Int)
    (1.25, 1.5) + (2.5, 2.25) // (3.75, 3.75) : (Double, Double)
    
    0 讨论(0)
提交回复
热议问题