Carl Hung
Carl Hung

Reputation: 555

Meta type of dictionary

I want to write a function that accepts any type to determine to do its job. but I encounter error with dictionary.type. it requests a specific type for the key of its placeholder.

is it any way I can do that?

as I may use like that:

foo(type: type(of: someDict)) // someDict can be any type of dictionary.
// can be [String: Any] or [Int, Any] or [Double: Any] or [Character: Any], etc. 


func foo(type: Any.Type) {
    switch type {
    case is Int.Type:
        print("is Int")
    //case is Dictionary: // error, Reference to generic type 'Dictionary' requires arguments in <...>
    case is Dictionary<Hashable, Any>: // also error, Using 'Hashable' as a concrete type conforming to protocol 'Hashable' is not supported
        print("is Dictionary")
    default:
        print("don't know")
    }
}

Upvotes: 0

Views: 94

Answers (2)

David Pasztor
David Pasztor

Reputation: 54716

First of all, you shouldn't be using Any as the type of the input argument to your function. Rather you should make your function generic.

To solve the issue with matching Dictionary's metatype, you should simply change Hashable to AnyHashable, which is the type-erased container type for a Hashable value.

func foo<T>(type: T.Type) {
    switch type {
    case is Int.Type:
        print("is Int")
    case is Dictionary<AnyHashable, Any>.Type:
        print("is Dictionary")
    default:
        print("don't know")
    }
}

Upvotes: 1

pacification
pacification

Reputation: 6018

Maybe you should work with Any not with Any.Type? This works fine:

let dict: [Int: Any] = [:]
foo(type: dict) // is Dictionary
foo(type: 12) // is Int
foo(type: "12") // don't know

func foo(type: Any) {
    switch type {
    case is Int:
        print("is Int")
    case is Dictionary<AnyHashable, Any>:
        print("is Dictionary")
    default:
        print("don't know")
    }
}

Upvotes: 0

Related Questions