4 ms·
abstract class Employee {} class Worker : Employee { public Worker(Person o) {...} } class Manager : Employee { public Manager(Employe
by platz 8y ago
abstract class Employee {}
class Worker : Employee {
public Worker(Person o) {...}
}
class Manager : Employee {
public Manager(Employee[] o) {...}
}
- sparkie 8y agoabstract sealed class Employee { enum TAG { Worker, Manager } readonly TAG tag; Employee(TAG t) { tag = t; } public class Worker : Employee { public Worker(Person p) : base(TAG.Worker) { ... } } public class Manager : Employee { public Manager(Employee[] o) : base(TAG.Manager) { ... } } } This is a bit closer to how it is really implemented. The constructors are internal to the outer type because it is sealed, making it a closed type. Interestingly C# rejects abstract & sealed on the same type, although this is accepted on the CLR. Discriminated unions are sometimes called tagged unions because of the way they're implemented. It's typically more efficient to check the tag on the object than it is to test its type. The tag is used when pattern matching over the object.
- platz 8y agoOk, the idea was to communicate to someone who has never seen an ADT before what kind of thing it is, rather than a full decompilation of how it's really implemented, but thanks for that too.