C#C
C#4y ago
28 replies
Thinker

✅ How to design an error interface

In my current project I have an interface called
ISymbol
. This interface has tons of derived interfaces (
INamedSymbol
,
ITypeSymbol
,
IFunctionSymbol
,
IVariableSymbol
, among others), which all can have multiple implementations. However, I also have an interface called
IErrorSymbol
which just represents an error which has been produced somewhere. There are several cases in which I have to cast between these interfaces, and many times I also have to perform
is
checks. The
IErrorSymbol
is meant to act as a more or less general-purpose interface which can describe any error, but which also can be used in place of any of the other interfaces.

Currently I have a general
ErrorSymbol : IErrorSymbol
implementation, as well as a couple specialized ones like
NamedErrorSymbol : INamedSymbol, IErrorSymbol
and
ErrorTypeSymbol : ITypeSymbol, IErrorSymbol
, although having one of these for every kind of error feels very annoying. What I would love to have is just a single (or at least very few) implementations which can serve the roll of any of the interfaces, however which don't necessarily implicitly implement those interfaces. Does that even make sense? Like, in my ideal world,
new ErrorSymbol() is ITypeSymbol
would be false, but
(ITypeSymbol)new ErrorSymbol()
would be acceptable. Is there any decent way to describe/implement this?
Was this page helpful?