fsharp / fsharp/fslang-suggestions

Add callables/invokables

Open
#1,085 18 comments 22 reactions 0 assignees View on GitHub
area: delegates-and-invokables
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

In discussion with @KathleenDollard the topic of callable/invokable objects came up. This isn't really advocating the suggestion, rather just creating a tracking issue for it with notes.

This exists in other languages but surprisingly this hasn't been requested for F# AFAICS so I thought I'd make some notes - it's not a totally terrible idea and is natural in machine learning programming and elsewhere.

For example a method called `Invoke` (possibly with an attribute) may make an object callable/invokable:

```fsharp
type Adder(c:int) =
member _.Invoke v = v + c

let adder = Adder(3)
adder 4 // short for adder.Invoke(4)
```
Now, if you can do the above then all of the following should really work too:
```fsharp
let adder1 = Adder 3
let adder2 = Adder 4

adder1(3)
adder 3
3 |> adder
List.map (adder1 >> adder2)
adder1 >> adder2
```

and be equivalent to:

```fsharp
//Callables:
let _ = adder1.Invoke(3)
let _ = adder1.Invoke 3
let _ = 3 |> (fun s -> adder1.Invoke(s))
let _ = List.map (adder1 >> adder2)
let _ = (fun s -> adder1.Invoke(s)) >> (fun s -> adder2.Invoke(s)) // Or would this make a new Adder?
```

Note the question in the last - would we go as far as altering `f >> g` to allow combinations of delegates, functions and callables, and if we did would the composition still always be a function?

Also note that delegates should be considered callables so `func.Invoke(arg)` can be `func arg`.

## Pros and Cons

The advantages of making this adjustment to F# are it allows for some nice machine learning model DSLs

The disadvantages of making this adjustment to F# are it adds subtlety and gives another rope to hang yourself on.

## Extra information

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

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
* [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

## 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

The issue names no files, tests, or implementation entry point. Start by reviewing the callable examples and the unresolved questions about Invoke, delegates, and function composition; work is complete only after the language behavior and scope are specified.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.