One type of operation that is not guaranteed to be available with a changed class and sometimes may be available with a changed structure is the ability to copy the state of a thing using an assignment operator. Using mutating interfaces with structures is difficult, since trying to use a mutating interface in a structure in a read-only context will produce code that compiles cleanly but does not work (I really want compilers to recognize the attribute for structure and interface elements that indicate that they should not be used in read-only structures). However, there are some contexts in which such a thing may be useful.
For example, if the algorithm required to save the state of the enumerator and returned the enumerator to its stored state, and if the enumerator was a common TEnum type, limited by both struct and IEnumerator<T>, it may be possible to copy the TEnum to a local variable of that type and then later copy it back. Note that this would work with some but not all to a local variable of that type and then later copy it back. Note that this would work with some but not all struct types which implement IEnumerable ; such a technique should in practice probably only be used with interfaces that explicitly document a requirement that all legitimate struct-type implementations must exhibit value semantics ( ; such a technique should in practice probably only be used with interfaces that explicitly document a requirement that all legitimate struct-type implementations must exhibit value semantics ( IEnumerator` does not have such a documented requirement).
Please note that the fact that the code can use the ability to copy structures by value when it exists, which excludes the possibility of using a slightly different (possibly less efficient) method using the implementation of the class interface. Feature Signatures:
void ActOnSequence<T>(T theEnumerator) where T:struct, IEnumerator<String>; void ActOnSequence(IEnumerator<String> theEnumerator);
can coexist and be used without difficulty. The former will be called to implement the value type of the interface, while the latter will be called when implementing the heap type. Note that without the restriction of struct it would be impossible to have both of these methods in scope and automatically call the correct method.
supercat
source share