
Peter Dimov wrote:
The compiler cannot check that the right tag is used since information is added to the exception at runtime, so you're still vulnerable to typos. That means you would still have to either put the tags you only declare them; and if you used different name than declared, compiler will catch that error. No, it will not, unless I'm misunderstanding something. You either have coupling and static checking, or you have no coupling and no static checking; strings or tags make no difference. A tag is just a compile-time string.
Tag is a type name recognized by compiler; you can declare it as many times and in as many locations as you wish. Important point about tag is that you have to declare it before you use it; compiler will enforce it. On the other hand, string is a literal; there is nothing compiler could check for you. Obviously, it means that while tags come with some coupling (although minimal - there is no need for "unified system" or "central location"), strings require no coupling at all. While this might sound like an advantage, I believe that it's not - strings (used as literals) are extremely prone to programming mistakes (like all literals). Obviously, there is simple cure: replace literal with single definition eg. macro or global const variable. But this solution comes at the cost, as you suddenly need two names: one for macro (or variable) and other for actual string. You also need guarantee that the later one is unique, and compiler will not help you with this. Tags do not have this problem - there is just one name, and language provides pretty good means (namespaces, nested types etc.) to help you make that name unique. The other problem with string is that you need its definition. Simplest solution for these problems is to keep all all string definitions in a single location - but that brings strong coupling. To sum it up : strings are prone to mistakes and there is no good cure for it; on the other hand, tags bring some (rather weak) coupling and that's it. Of you want, you can introduce requirement that tag type has to be fully defined at the point of use - compiler will provide more warranties (more difficult to define mispelled tag) and coupling will be still weaker that strings used as macros or variables (no need for central location to warranty uniqneness if you have sane design). I'm have experience with similar system, my colleagues are still figthing with problems introduced by literals used as identifiers, and we have not found good way to solve them so far. Compiler provides too few checks, and infrastructure to maintain uniqueness is too complex (and centralized). B.