nalyd88
nalyd88

Reputation: 5148

Generic type constraint for numerical type only

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, etc., but not string. Thanks for any help.

Upvotes: 31

Views: 10890

Answers (3)

Aky
Aky

Reputation: 1817

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)

Upvotes: 1

DeFrenZ
DeFrenZ

Reputation: 2252

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.

Upvotes: 12

Martin Gordon
Martin Gordon

Reputation: 36399

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)

Upvotes: 29

Related Questions