Skip to content

feat(all): add Compiler.is* target detection flags for conditional branching#4692

Merged
MangelMaxime merged 4 commits into
mainfrom
conditional-target-branching
Jun 27, 2026
Merged

feat(all): add Compiler.is* target detection flags for conditional branching#4692
MangelMaxime merged 4 commits into
mainfrom
conditional-target-branching

Conversation

@MangelMaxime

Copy link
Copy Markdown
Member

Fable's conditional compilation relies on compiler directives. It is working fine but has a huge limitations.

In IDE, you often end up with no IDE support most of the compiler directives branches as the compiler directives is not set in the fsproj. And even when setting it up in the fsproj the experience is not always great depending on the IDEs.

This PR is building on top of the existing foundation in Fable used for Compiler.debugMode or Compiler.triggeredByDependency.

The idea is that in Fable, if a if ... then ... else ... structure is using boolean constant in their condition then Fable auto erase unreachable branches.

This now allow us to write things like

open Fable.Core

let greet name =
    if Compiler.isJavaScript then
        $"Hello from JS, {name}!"
    elif Compiler.isPython then
        $"Hello from Python, {name}!"
    else
        $"Hello from .NET, {name}!

which compiles to

JavaScript

 export function greet(name) {
      return `Hello from JS, ${name}!`;
  }

Python

def greet[_A](name: _A=UNIT) -> str:
      return ("Hello from Python, " + str(name)) + "!"

The only impact is on .NET where there will be a real runtime check against the boolean. This should be more than fast enough and people can use Compiler.isDotnet to avoid having to check all the conditions.

Summary

Thanks to these changes, we are now able to provide a better experience for interop/universal code.

@github-actions

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 34
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_hash_set.py 18 Excluded
temp/tests/Python/test_applicative.py 12 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 2 Excluded

@MangelMaxime

Copy link
Copy Markdown
Member Author

Hello @ncave,

my memory is failing.

In order to make, fable-standalone tests to pass I need to regenerate the fable-metadata for Fable.Core.

I don't remember how to do that, is it as simple as copying the result of dotnet build -c Release over their ?

@ncave

ncave commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

@MangelMaxime
Fable.Core doesn't actually have code, just bindings/metadata, so you can just use it as is. Just replace the one in fable-metadata with latest (as trimming it will not save much anyway).

@MangelMaxime MangelMaxime merged commit 621dea3 into main Jun 27, 2026
24 checks passed
@MangelMaxime MangelMaxime deleted the conditional-target-branching branch June 27, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants