fsharp / fsharp/fslang-suggestions

Support base methods in closures

Open
#1,009 2 comments 9 reactions 0 assignees View on GitHub
approved-in-principle area: object-programming
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we support
```fs
open System
type B() =
abstract K : int -> unit
default _.K _ = ()
type C() =
inherit B()
override _.K _ = ResizeArray().ForEach(Action<_> base.K)
// error FS0408: The 'base' keyword is used in an invalid way. Base calls cannot be used in closures. Consider using a private member to make base calls.
// error FS0419: 'base' values may only be used to make direct calls to the base implementations of overridden members
```

The existing way of approaching this problem in F# is making a private member as suggested.

However, the C# compiler does this for us and we don't have to workaround it.
https://sharplab.io/#v2:CYLg1APgAgzABFATHAQnA3g+A3AlgJwBcBXAQwBsEAWOAaQApcA7QuAfQEoMBfObgWABQsBMgDCcEKgxC4crHAD22AKb58uYCup1GLdl3Sz5JpioDuCAIxUAdABlcAZ0IAeZoQB89DrYBiivgAoqQAxgAW9ABGpE4qtrQcANzGcgKC3EA===

F# can do the same.

## Pros and Cons

The advantages of making this adjustment to F# are
1. Conciseness
2. Convenience
3. Consistency with C#

The disadvantage of making this adjustment to F# is that this may be an already-made decision in previous versions of F#.

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [ ] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the F# example and comparing its closure behavior with the linked SharpLab C# example. Define the language-design and compatibility implications, then verify that base method calls work inside closures without the current FS0408 and FS0419 errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.