[Pitch] Properties and subscripts with optional getters

Recently I thought about more general feature that allows to specify different types for getter and setter, which was already a bit discussed Different types for getter and setter

This allows to cover more cases than just making returned value Optional.

One edge case is with Never:

  • for setter Never type is meaningless, it is effectively a get-only subscript. The question is will it be useful for generic contexts.
  • for getter it is also meaningless, but in another direction. Practical usage of get that never returns is like a fatalError. Truly instead of this we need a set-only subscript.

So the design I have come into is:

  1. getter and setter can be return completely different types
  2. introducing a set-only subscripts
  3. For getters Never type is disallowed, it is a compiler error. This restriction can be lifted later if it become useful
  4. For setters Never type might be allowed, but this need to discussed.
  5. If getter and setter have the same return type, it a compiler error with suggestion to use current syntax.

Currently setter allows to specify the name of the argument. This can be extended to also allow specifying the Type for getter and setter. The syntax is like the following:

subscript(key: Key) {
  get(String) { ... }
  set(Any) { ... }
}

subscript(key: Key) {
  get(Never) { ... } // compiler Error, returning Never is disallowed for subscript gtter
  set(Any) { ... }
}

subscript(key: Key) {
  set(Value) { ... } // set-only subscript
}

subscript(key: Key) {
  get(String) { ... }
  set(Never) { ... } // compiler Error, use get-only subscript instead
}

subscript(key: Key) { 
  // compiler Error, return type should be specified at subscript declaration 
  // instead of at getter / setter separately 
  get(Value) { ... }
  set(Value) { ... }
}