Last active
November 30, 2023 09:48
-
-
Save ForNeVeR/135addb5c95faaba80e8a122dcaf7392 to your computer and use it in GitHub Desktop.
This file contains 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
open System | |
let mutable debug = true | |
type T = T with | |
static member inline ($) (T, arg: unit) = () | |
static member inline ($) (T, arg: int) = 0 // mandatory second terminal case; is unused in runtime but is required for the code to compile | |
static member inline ($) (T, func: ^a -> ^b): ^a -> ^b = | |
fun (_: 'a) -> T $ Unchecked.defaultof<'b> | |
let inline negate(): 'a = | |
T $ Unchecked.defaultof<'a> | |
let inline debugLog format = | |
if debug then | |
Printf.kprintf ( | |
printfn "%A: %s" DateTime.Now | |
) format | |
else negate() | |
[<EntryPoint>] | |
let main _ = | |
debug <- false | |
debugLog "This: %s" "foo" | |
debugLog "That: %d - %d = %d" 2 2 4 | |
debugLog "Invisible! %s" "haha" | |
debug <- true | |
debugLog "Visible" | |
0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You said function, not method. So ParamArray is not an option.
The Haskell code you linked is based on the inferred result type.
Here's a way to resolve based on the inferred result type in F#:
UPDATE
The above code doesn't work anymore as from F# 4.1 (see the comments) but here's a better example with a recursive polyvariadic function taking n (unlimited) arguments:
You can also have a look at this polyvariadic fold.