fsharp / fsharp/fslang-suggestions

Conditional let-bound functions

Open
#846 12 comments 0 reactions 0 assignees View on GitHub
approved-in-principle area: core-functional
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

I propose we allow conditional let-bound functions.

The existing way of approaching this problem in F# is having a helper class for the conditional method.
```fs
module M =
type private HelperClass =
[]
static member Log (x:string) = System.Diagnostics.Debug.WriteLine x

// FS0826: The 'ConditionalAttribute' attribute may only be used on members
let [] private log (x:string) =
System.Diagnostics.Debug.WriteLine x
```

## Pros and Cons

The advantages of making this adjustment to F# are
1. Consistency - `let`-bound functions in modules compile to methods. What makes them not allowed to be conditional?
2. Conciseness - A helper class just to define a helper function?

The disadvantages of making this adjustment to F# are none that I can think of.

## Extra information

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

Related suggestions:
https://github.com/dotnet/fsharp/issues/8689 - In contrast, user-defined method-only attributes can be placed on `let`-bound properties and literals??

## 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
* [x] 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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the proposed example and the related suggestion in issue #8689, then trace the F# compiler's handling of ConditionalAttribute on let-bound functions. Done means a private module-level let-bound function can use ConditionalAttribute without requiring the helper class shown in the issue, while retaining conditional behavior.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.