Better implementation of hash for constrained types