Implementing magic `with` function

As I recall, someone else attempted to implement this a few years ago, but how did the KeyPath proposal add magic members to Any? I'm trying to do it for this proposal.

I implemented the [keyPath:] subscript as a special case in name lookup, introducing it as an extra overload choice when a matching subscript expression is type checked:


Can we briefly discuss why extensions of Any are not supported? I am aware that one reason is that Any is not a nominal type because tuples are also Any. However, there is a desire to support extensions for non-nominal types anyway.

Are there any other reasons for intentionally avoiding the allowance of extensions for Any?

If there are no other reasons, it could be a significant goal to move away from the magic nature of Any:

  • Make Any a real protocol as part of the stdlib.
  • Support extensions for structural types.
  • Allow extensions for Any.

An alternative approach could be to introduce the @_marker protocol Nominal {} and make all nominal types implicitly conform to it, similar to how Sendable conformance is handled.

Is that same function in CSSimplify used for all name lookup or just subscripts? And if I changed that function manually to see if a member named with was called, how would I make that call the correct implementation?

I think it's a different location, but you should be able to find a similar function in the same part of the codebase that deals with member lookup.

You could start another thread about that.

1 Like