Last active
July 21, 2026 14:08
-
-
Save TheAngryByrd/24e7862b3a5374e3ad3ae2256b940b7c to your computer and use it in GitHub Desktop.
F# Ignore Explicity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// RequiresExplicitTypeArguments forces each caller to name the type it discards, | |
| /// as in `ignore<int> x`. The compiler then checks that the value has that exact type. | |
| /// Plain `ignore` infers its type argument and silently accepts any value, so it hides | |
| /// a common mistake: a caller can ignore a function value after forgetting an argument, | |
| /// or ignore a Task or Async that the caller meant to await. The explicit type argument | |
| /// turns each of these mistakes into a compile error. | |
| [<AutoOpen>] | |
| module Ignore = | |
| open System.Threading.Tasks | |
| let private completedUnitTask = Task.FromResult () | |
| [<RequiresExplicitTypeArguments>] | |
| let inline ignore<'a> (x: 'a) : unit = ignore<'a> x | |
| type Async with | |
| [<RequiresExplicitTypeArguments>] | |
| static member inline Ignore<'a>(x: Async<'a>) : Async<unit> = FSharp.Control.Async.Ignore<'a> x | |
| type System.Threading.Tasks.Task with | |
| [<RequiresExplicitTypeArguments>] | |
| static member inline Ignore<'a>(x: Task<'a>) : Task<unit> = | |
| if x.Status = TaskStatus.RanToCompletion then | |
| completedUnitTask | |
| else | |
| task { | |
| let! r = x | |
| return ignore<'a> r | |
| } | |
| [<RequiresExplicitTypeArguments>] | |
| static member inline IgnoreArg<'a>(x: Task<'a>) : System.Threading.Tasks.Task = x | |
| static member inline ToArg (x : Task) : Task<unit> = | |
| if x.Status = TaskStatus.RanToCompletion then | |
| completedUnitTask | |
| else | |
| task { | |
| do! x | |
| return () | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment