Skip to content

Overriding the default() operator. #71

@orthoxerox

Description

@orthoxerox

I know there's been a discussion on StackOverflow involving Eric Lippert on the potential problems with overriding the default() operator, but could it be implemented with additional restrictions?

  1. Overridden default() operator can return only a static readonly instance of the class or its subclass that have to be immutable. If the class is mutable, then the immutable subclass cannot access any base() methods or write to any fields/properties. Uh, I guess this is a whole 'nother issue, declaring immutability and asserting it by the compiler.
  2. If it's a struct, parameterless constructor must be equivalent to default().
  3. If it's a class, the compiler initializes all references to default() instead of null (I know it's slower, since you can't simply XOR the pointer with itself, but it's just copying a single value). Assigning null to it is rewritten to assigning default(T) by the compiler, the same with checking if it's null.

This way the consumer of the new class won't notice anything except the lack of NRE's.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions