-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implicit interface implementation from an object expression #555
Comments
@ntwilson this is interesting feature, could you precise what would happen in such case: type IContainer<'t> =
abstract member DoesNotContain : 't -> bool
let helloWorld = "hello world!"
let container1 = { new IContainer<string> helloWorld } I'd like to see how the compiler would help me fix my mistakes if I'm doing something stupid. |
Ideally, since the type of |
Kind of related #524 |
I think this is very related too: #132 |
Also kind of related: #561 since it calls for implicit implementation of an interface (specifically, |
I will close this as a duplicate of #524 |
I am not sure about #524 but does that suggestion and really capture what is proposed here? |
@dsyme Can you clarify why you reopened. What is not captured by #524? I don't see it, they look identical to me. Is it that this suggestion doesn't allow to use Anyway, after looking at all the other suggestions I suggest to use the same syntax here (adding the let container1 = { new IContainer<string> by helloWorld } |
Closing in favour of #524 after all |
I propose we allow an object expression to satisfy an interface implicitly, given some object that already implements all of the members given in the interface. For example:
The syntax here is unimportant, I just went with the most minimal syntax possible.
I believe this would be related to suggestion #195 for normal implicit interface implementation in classes, but I haven't actually looked at the F# compiler source.
(If anyone has used impromptu-interface, this would allow you to do something similar, but would be statically checked by the compiler for correctness).
The existing way of approaching this problem in F# is to list out each member in the object expression, e.g.
For this example, this is rather trivial, but when dealing with interfaces with many members, it can get cumbersome quickly.
Alternatively, you could use Reflection a la impromptu-interface, but this forgoes any type checking.
Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are
Extra information
Estimated cost: I haven't actually cracked open the F# compiler source, but I would guess that if #195 were implemented, this would be a minor addition.
Related suggestions: #195
Affadavit (must be submitted)
Please tick this by placing a cross in the box:
Please tick all that apply:
The text was updated successfully, but these errors were encountered: