digitalmars.D.learn - Is Key Type?
- Xinok (5/5) Aug 02 2015 is there a trait in D or Phobos which will tell you if a type can
- Gary Willoughby (17/22) Aug 02 2015 import std.stdio;
is there a trait in D or Phobos which will tell you if a type can be used as a key for an associative array? For example, where T is some type: static assert(isKeyType!T) int[T] hashTable = ...
Aug 02 2015
On Sunday, 2 August 2015 at 17:55:16 UTC, Xinok wrote:is there a trait in D or Phobos which will tell you if a type can be used as a key for an associative array? For example, where T is some type: static assert(isKeyType!T) int[T] hashTable = ...import std.stdio; enum isKeyType(T) = __traits(compiles, int[T]); class Foo {} struct Bar {} void main(string[] args) { // Success assert(isKeyType!(int)); assert(isKeyType!(string)); assert(isKeyType!(Foo)); assert(isKeyType!(Bar)); // Fail assert(isKeyType!(void)); assert(isKeyType!(delegate(){})); assert(isKeyType!(function(){})); }
Aug 02 2015