none
Type.GUID anomaly. RRS feed

  • Question

  • I have just discovered a bug in our code, caused by a subtle issue with generics and GUID.

    It turns out the GUID for a Type X<T> is the same for X<Int32>, X<Double> in fact X<AnyType> !

    Because I assumed these kinds of types are different I assumed each type would have different GUID, but they do not.

    Is there any other uniqe ID associated with instances of generic types that is different in each case?

     

    Thx

    Cap'n

    PS: we use the GUID as a key into a dictionary that contains metadata related to a type that is time consuming to evaluate. So by calling some helper and passing Type it just does a lookup in the dictionary to get the info (except for the first time for any type, cos it must calculate the data to put in dictionary).

    However because DatumInfo<Int32> has the same GUID as say DatumInfo<Double> this all falls apart.

     

     

     


    Sunday, August 21, 2011 9:51 PM

Answers

  • Why use a GUID why not use the signature of the type itself (or a hash of that signature)?
    • Marked as answer by Paul Zhou Monday, August 29, 2011 6:08 AM
    Sunday, August 21, 2011 10:07 PM
  • "Because I assumed these kinds of types are different I assumed each type would have different GUID, but they do not."

    AFAIK, these ids are generated at compile time, and since generics are "instantiated" at run time it doesn't seem surprising that they use the same id.

    "So by calling some helper and passing Type it just does a lookup in the dictionary"

    So use Type as dictionary key. Not only that it doesn't have the id problem but it's also likely to be faster because it's a reference type unlike Guid which is a (rather large) value type.

    • Marked as answer by Paul Zhou Monday, August 29, 2011 6:08 AM
    Sunday, August 21, 2011 10:14 PM
    Moderator

All replies

  • Why use a GUID why not use the signature of the type itself (or a hash of that signature)?
    • Marked as answer by Paul Zhou Monday, August 29, 2011 6:08 AM
    Sunday, August 21, 2011 10:07 PM
  • "Because I assumed these kinds of types are different I assumed each type would have different GUID, but they do not."

    AFAIK, these ids are generated at compile time, and since generics are "instantiated" at run time it doesn't seem surprising that they use the same id.

    "So by calling some helper and passing Type it just does a lookup in the dictionary"

    So use Type as dictionary key. Not only that it doesn't have the id problem but it's also likely to be faster because it's a reference type unlike Guid which is a (rather large) value type.

    • Marked as answer by Paul Zhou Monday, August 29, 2011 6:08 AM
    Sunday, August 21, 2011 10:14 PM
    Moderator
  • "Because I assumed these kinds of types are different I assumed each type would have different GUID, but they do not."

    AFAIK, these ids are generated at compile time, and since generics are "instantiated" at run time it doesn't seem surprising that they use the same id.

    "So by calling some helper and passing Type it just does a lookup in the dictionary"

    So use Type as dictionary key. Not only that it doesn't have the id problem but it's also likely to be faster because it's a reference type unlike Guid which is a (rather large) value type.


    Darn, your both correct !

    GUID was used historically, some years ago and the original reason has I think long since gone.

    I'm gonna review the logic and use Type, thaks for heads up !

     

    Cap'n

     

    Monday, August 22, 2011 2:02 PM