-
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
Inline Interfaces, i.e. simpler SRTP #641
Comments
actually this in a way already works type Duck< ^a
when ^a: (member Walk: unit -> unit)
and ^a: (member Quack: unit -> unit) > = ^a it doesn't however play very nice with tooling imo |
Given the current tooling, even using the above type abbreviations, the inline functions would still have to be written using the verbose and sometimes confusing to remember SRTP syntax. let inline walkAndQuack (duck: ^a Duck) =
(^a : (member Walk: unit -> unit)(duck))
(^a : (member Quack: unit -> unit)(duck)) At this point, there is no real point in having that type abbreviation today. If the type abbreviation could allow the simpler member access, that would also reduce the need for creating the new inline interface syntax that I proposed. Either the type abbreviation syntax or the inline interface syntax would work just as well, so long as it results in the simpler and intuitive member access. I am now reminded of an earlier suggestion that if a member declares in it's definition that a type constraint have a member, that the member can be accessed with the simpler member access. I don't remember which issue number it is, nor can I think of how to search for it. Assuming the type abbreviation implementation, this suggestion would now be an amendment to that suggestion. |
@TheJayMann wrote:
Is #440 (which has been approved in principle and now has an RFC) the one you were thinking of? |
This is exactly the one I was thinking. Perhaps, if this issue is approved, rather than being a new RFC, the existing RFC can be updated? The advantage of the suggestions I had made is that if multiple inline functions were looking for the same member definitions, those method definitions could be made one time, rather than making the same member definitions multiple times. Unless, of course, there were already plans to allow the type abbreviations in the RFC, at which point this is a duplicate. |
This is a really neat idea. |
I have completely avoided using SRTPs so far, because the syntax is just too ugly: difficult to read, which means difficult to maintain — which feels counter to one of F#'s great strengths (code simplicity which leads to ease of maintenance). If this suggestion was accepted and implemented, I'd feel safe actually using SRTPs, since I wouldn't feel that I'd be creating a maintenance nightmare for myself down the road. |
Would it be possible to have the same or similar feature (inline keyword or attributes) for records of functions (for records that only have functions)? Recenty there was a twitter thread about the preferred abstractions interfaces vs records of functions here, and it seems each have their own user base: https://twitter.com/shanelogsdon/status/962786920551079936 Something similar feature would be something like this: type inline FDuck = {
Walk: unit->unit
Quack: unit->unit
}
[<Struct>]
type inline StructFWalk = {
Walk: unit->unit
Quack: unit->unit
}
Right now in average there is a huge ~5x-6x performance hit when the functionsa are passed as records instead of passed directly on call intensive workload (low latency event processing with 100M+ event per second). Passing lot's of functions individually around due this performance hit makes the code unreadable. |
I've started an implementation already, see this PR dotnet/fsharp#4726 |
Treating as duplicate of #1089 |
I propose the concept of inline interfaces. They would be defined similar to how interfaces are currently defined, but with an inline keyword.
The interface can then be used in an inline function or method.
This would be a simpler way to write the equivalent of how SRTPs are done currently.
Extra information
Related suggestions:
#207 Support anonymous record types [ RFC FS-1030 ]
I am not entirely certain, but I believe the concept of anonymous records or tables may also be attempting to address this issue.
Affidavit (please submit!)
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: