Can I specify that a generic is a value type?

2020-06-01 04:54发布

I know that we can essentially specify that our generics be any reference type by using AnyObject:

class Foo<T: AnyObject> {
    // ...
}

But is there a way to specify that our generics should only be value types, and not allow reference types?

2条回答
女痞
2楼-- · 2020-06-01 05:35

A way to use Assertion:

public final class Synchronized<T> {
    private var _value: T        

    public init(_ value: T) {
       assert(!isReferenceType(value))
       _value = value
    }
    ...
}

func isReferenceType(_ any: Any) -> Bool {
    return Mirror(reflecting: any).displayStyle == .class
}

Unit Tests:

import XCTest

final class SynchronizedTests: XCTestCase {
    class FakeClass {}
    struct FakeStruct {}
    enum FakeEnum {
        case first
    }

    func testIsReferenceType() {
        XCTAssert(isReferenceType(FakeClass()))

        XCTAssertFalse(isReferenceType(FakeStruct()))
        XCTAssertFalse(isReferenceType(FakeEnum.first))
        XCTAssertFalse(isReferenceType("string"))
        XCTAssertFalse(isReferenceType(123))
        XCTAssertFalse(isReferenceType(123.4))
        XCTAssertFalse(isReferenceType([1, 2, 3]))
        XCTAssertFalse(isReferenceType(Set([1, 2, 3])))
        XCTAssertFalse(isReferenceType(["1": 1, "2": 2, "3": 3]))
    }
}
查看更多
够拽才男人
3楼-- · 2020-06-01 05:47
// some code for testing    
class C { } // just a simple class as an example for a reference type
var c = C()
var d: Double = 0.9 // a value type

Solution 1 via extension

protocol ValueType { }
extension Double : ValueType { }
extension Int : ValueType { }
// ... all value types to be added

func printT1 <T: ValueType> (input: T) {
    println("\(input) is value")
}
printT1(d) // Does work
//printT1(c) // Does not work

But as mentioned in the comments, it is working but not feasible, because user defined value types have to implement this protocol.


Solution 2 via method signature

func printT <T: AnyObject> (input: T) {
    println("\(input) is reference")
}

func printT <T: Any> (input: T) {
    println("\(input) is value")
}

Solution 3 via assert

Another solution could be via assert

func printT <T: Any> (input: T) {
    print("\(input) is " + ((T.self is AnyObject) ? "reference" : "value"))
}

"Solution" 4 via where clauses

This would be the best solution, I think. Unfortunately, it is not possible to have

func printT <T: Any where T: ~AnyObject > (input: T) {
    println("\(input) is value")
}

or similar. Maybe it will be possible in future releases of Swift.

查看更多
登录 后发表回答