Extension to all types

Hi. Is it possible to make and extension to all types? I'm trying to do something like this:

extension Any { // Non-nominal type 'Any' cannot be extended
  public func apply<T>(_ function: (Self) -> T) -> T { function(self) }
}

// using:
let values = someVeryVeryVeryLongExxpression
  .filter { filter conditions }
  .map { $0.propertyName }
  .sorted()
  .apply(OrderedSet.init)

Extension to Any is provided as an example. The compiler emits error as expected in this case.

Currently, we can make such extension to AnyObject which covers all Classes and Actors.
Is it possible to make such extension on all value types?

Of course, I can make extension to protocol like Collection. The problem appears when this extension is implemented in other protocols like Sendable, CustomStringConvertible and others because using of apply function becomes ambiguous.

2 Likes

No

Is there any arguable reasons for such language design or it is temporary limitation in type system?