support "proper" delegation pattern #17830
Replies: 0 comments 4 replies
-
related feature requests:
|
Beta Was this translation helpful? Give feedback.
-
related discussions: |
Beta Was this translation helpful? Give feedback.
-
I think this was discussed before on contributors but my preferred syntax would be: export (impl: MyTrait1).* which would be roughly equivalent to: val impl1: MyTrait1 = impl
export impl1.* except it should retain path-dependency through |
Beta Was this translation helpful? Give feedback.
-
Agree, the proposed syntax |
Beta Was this translation helpful? Give feedback.
-
See https://www.wikiwand.com/en/Delegation_pattern
The current
export
clause feature allows to achieve it only partially / not smoothly.Currently, there are two options with
export
:export fieldDelegateTo.{unrelatedMember => _, _}
export fieldDelegateTo.{foo1, foo2, ... fooN}
The first option is not only a boilerplate, it has a high risk of exporting too many unrelated public members.
You have to explicitly list all members to "mute". You can easily skip some.
If a new member is added to the class, most likely you will forget to update all the exports.
It may lead to context pollution.
It will potentially increase compilation time, show a lot of unexpected members in the completion list in IDE, etc...
The second option is also a boilerplate code.
You have to list all the members.
You can accidentally export some unrelated member.
You have to update all exports in case a new method is added to the trait.
Without "proper" delegation,
export
feels a little bit incomplete.The motivation for
export
was to make "composition over inheritance" require less boilerplate code.It's very likely that wildcard import will be overused (cause programmers are lazy), leading to all the cons listed above.
We need some new syntax, which would allow us to only export methods of a specific trait.
Something like
export delegate.{_: MyTrait1}
:From the first glance, this feature doesn't require a lot of effort (patch import selector parsing and restrict filtering of exported methods in
Namer.scala
).The most difficult is as always to agree on the syntax.
Beta Was this translation helpful? Give feedback.
All reactions