fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Allow interfaces to be implemented by expressions

Open baronfel opened this issue 9 years ago • 9 comments

Submitted by Ivan J. Simongauz on 9/3/2016 12:00:00 AM
1 votes on UserVoice prior to migration

Implement interface delegating by next syntax:

type MyType() =
    let delegator : IAddingService
    interface IAddingService by delegator with
        member this.Add x y = // <- this is override
        x + y

Special behavior when event in interface - sender substitution

Original UserVoice Submission Archived Uservoice Comments

baronfel avatar Oct 20 '16 01:10 baronfel

Related: https://github.com/dotnet/csharplang/issues/477

smoothdeveloper avatar Apr 26 '17 12:04 smoothdeveloper

I think this is an interesting idea, that hasn't gotten much attention. Related (directly inspired by?) kotlin: http://kotlinlang.org/docs/reference/delegation.html

Much in the kotlin example also works out with F#, e.g. delegating to a constructor argument:

type DelegatedAdd(adder:IAddingService) =
    interface IAddingService by adder

kurtschelfthout avatar Aug 29 '17 21:08 kurtschelfthout

I think this is an interesting idea, that hasn't gotten much attention. Related (directly inspired by?) kotlin: http://kotlinlang.org/docs/reference/delegation.html

Not inspired by, but it's nice to see. Have been thinking of this from before Kotlin was even a twinkle in JetBrain's eye :)

dsyme avatar Aug 29 '17 22:08 dsyme

Hrm, what about something slightly more general purpose. I'd like to be able to do this with object expressions.

Rickasaurus avatar Aug 29 '17 23:08 Rickasaurus

And records. Both in terms of value and structure.

isaacabraham avatar Aug 30 '17 07:08 isaacabraham

Other implementations:

  • Dotty has released a version of this featured called export in version 0.14: https://dotty.epfl.ch/blog/2019/04/15/14th-dotty-milestone-release.html

baronfel avatar Apr 15 '19 18:04 baronfel

I suggest that the expression after by doesn't require to be of the interface type. Instead, the compiler should forward all possible methods and error on missing implementations (the suggestions already shows how to provide them). This would extend this suggestion to include #195, see https://github.com/fsharp/fslang-suggestions/issues/195#issuecomment-499268087 This might complicate the implementation a bit, but I think it would be well worth it.

matthid avatar Jun 05 '19 21:06 matthid

I just read an article about using flexible types for dependency injection and I believe that that syntax would greatly help in making a type that merges multiple implementations into a single environment object.

Nhowka avatar Dec 06 '20 15:12 Nhowka

What if we use an equals sign instead of by? There is an earlier example of how it could look here that feels quite natural: https://github.com/dotnet/csharplang/issues/288#issuecomment-288202651

diegoaltb avatar Jul 05 '21 18:07 diegoaltb