fsharp / fsharp/fslang-suggestions

Function pointers

Open
#934 34 comments 12 reactions 0 assignees View on GitHub
area: delegates-and-invokables area: interop
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Function pointers

I propose we offer mechanisms to create, consume, and invoke function pointers, along the lines of the new constructs available in C# 9.0: https://github.com/dotnet/csharplang/blob/master/proposals/csharp-9.0/function-pointers.md

The existing way of approaching this problem in F# is nonexistent. It may be possible to write a separate C# assembly when these features are needed and call methods in that assembly from F#, but in most cases this would probably introduce additional burdens of benchmarking and IL code inspection to verify whether the cost of this indirection negates the benefit of faster invocation of the functions referenced via function pointers in the C# code.

## Pros and Cons

The advantages of making this adjustment to F# are expanding the opportunities for writing comprehensible and maintainable code that generates high-performance functionality at runtime without resorting to here-be-dragons features of .NET like ILGenerator. F# arguably offers the most pleasant experience in the .NET language ecosystem for this style of development, so it would be nice to have feature parity with C# in writing code that compiles to faster IL opcodes.

The disadvantages of making this adjustment to F# are exposing new potentially unsafe constructs in a language that doesn't have `unsafe` blocks like in C#. But such constructs already exist and it is considered the responsibility of programmers and library users to be aware of the implications of their use.

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): M -- although this may be a tricky new feature for F#, the C# compiler may provide a starting point for a syntax proposal, a reference implementation, and possibly some reusable code. It's not clear whether these features are fully finalized as of the C# 9.0 release, so we may want to wait until they are considered completely finished to take advantage of the C# team's code, experience, and documentation.

## 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
* [ ] 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 reading the linked C# 9.0 function-pointers proposal and compare its constructs with F#'s existing language capabilities. No implementation files or tests are named; done would require an agreed F# syntax and design for creating, consuming, and invoking function pointers, plus an implementation and validation plan.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.