I use interface classes a lot. But I usually just have a single concrete class (excluding tests) that implements them.
I was curious if anyone knows if compiled dart code somehow merges the classes into one, since effectively the dart compiler can know that any interface of a given class will always only be the concrete implementation.
Example:
abstract interface class _Context {
T get<T>(String field);
}
class _ContextImpl implements _Context {
final Map<String, Object> _index;
_ContextImpl(this._index);
T get<T>(String field) => _index[field] as T;
}
Would dart here know that _Context only has a single concrete implementation, so it would simply erase _Context and just use _ContextImpl? Effectively, rewriting the code as:
typedef _Context = _ContextImpl;
class _ContextImpl {
final Map<String, Object> _index;
_ContextImpl(this._index);
T get<T>(String field) => _index[field] as T;
}
Additional context, I usually define an interface for everything that isn’t strictly a data type, because I like to limit myself to only the methods I actually use / need. I usually also do this for private classes. I also find it easier to understand what I’m doing if there is a compact interface before the actual implementation.
Example above is simple. I could just define a single _Context. But then my IDE would suggest _index, even though I never want to modify the property outside the actual class.