Description
Extension methods (#41) can extend the set of instance members of an interface type T with a set of members declared outside of T. We may describe this as a zero-cost abstraction, because the denoted instance of type T is used directly (without an additional wrapper object, or any other kind of run-time support), and because the extension members are resolved statically (such that extension member invocation can be even faster than instance method invocation). In short, extension methods allow us to add convenience functions operating on instances of T, and invoking them using the syntax of instance member invocations.
We may also wish to replace the set M of instance members of an interface type T by a set Malternate of members declared outside of T. The two sets may overlap, but the point is that some of the instance members of T should not be used, or they should only be used in a specific way. A zero-cost abstraction mechanism that allows us to perform this replacement could help enforcing this particular kind of discipline without lowering the performance: If an instance member shouldn't be invoked at all then it would be omitted from Malternate, and if certain instance members can be called, but only in specific ways, then there would be some members in Malternate whose bodies will call those instance members in the appropriate manner. In short, this kind of abstraction would allow us to impose an added discipline on the use of an instance of type T and invoke them using the syntax of instance members.
For instance, if a given JSON value is modeled using instances of List<dynamic>
, Map<String, dynamic>
, bool
, String
, or numbers, and the JSON value is intended to conform to a given schema, then we could use this kind of abstraction to ensure that the object representing the JSON value is not modified (by not including any mutating members), and that it is only accessed in a way which is consistent with the schema (by providing member declarations that will access the parts of the value according to their schemas).