fsharp / fsharp/fslang-suggestions
Expose GetInvocationList from F# event
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
# Title of Suggestion
I have an expensive logic that I want to execute only if there are any subscribers to the event. Like this
``` F#
let modelTypeChanged = Event()
[]
member val ModelTypeChanged = modelTypeChanged.Publish
member vm.CurrentModel
with get () : 'model = currentModel
and private set (newModel : 'model) =
// TODO:
//if currentModel <> newModel then
let oldModel = currentModel
currentModel <- newModel
notifyPropertyChanged "CurrentModel"
let ``delegate`` = modelTypeChanged |> getDelegateFromEvent
match ``delegate`` with
| null -> ()
| _ when ``delegate``.GetInvocationList () |> (not << Seq.isEmpty) ->
let oldModelType = oldModel.GetType ()
let newModelType = newModel.GetType ()
match FSharpType.IsUnion newModelType, FSharpType.IsUnion newModelType with
| true, true ->
let oldUnionCase, _ = FSharpValue.GetUnionFields(oldModel, oldModelType)
let newUnionCase, _ = FSharpValue.GetUnionFields(newModel, newModelType)
if oldUnionCase.DeclaringType <> newUnionCase.DeclaringType
|| oldUnionCase.Name <> newUnionCase.Name
then modelTypeChanged.Trigger (vm, EventArgs ())
| false, false ->
if oldModelType <> newModelType
then modelTypeChanged.Trigger (vm, EventArgs ())
| _ -> modelTypeChanged.Trigger (vm, EventArgs ())
| _ -> ()
//else ()
```
But for now Microsoft.FSharp.Control.Event type or Microsoft.FSharp.Control.IEvent<'Delegate,'Args> interface do not expose `GetInvocationList` method.
My current solution looks like this
``` F#
static let multicastFiled = typeof>.GetField("multicast", BindingFlags.NonPublic ||| BindingFlags.Instance)
let getDelegateFromEvent (event : Event) = multicastFiled.GetValue event :?> EventHandler
```
### The advantage of making this adjustment to F# is the ability to write more performant code regarding .NET events.
### The disadvantages of making this adjustment to F# are absent.
## Extra information
Estimated cost (XS, S, M, L, XL, XXL): XS
https://github.com/dotnet/fsharp/issues/10380#issuecomment-736584904
## 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
Start with Microsoft.FSharp.Control.Event and the Microsoft.FSharp.Control.IEvent<'Delegate,'Args> interface, then read the linked discussion for the API expectations. Done means subscriber invocation-list information is publicly available without the private reflection workaround, with the resulting behavior verified against the existing event implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100