Created
September 26, 2023 10:30
-
-
Save MelbourneDeveloper/fa13caea79818e1a54b601e1137f3980 to your computer and use it in GitHub Desktop.
F# Factory With Type Inference
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
module Tests | |
open Xunit | |
type Factory<'T> = unit -> 'T | |
let factoryFunction<'T> () : 'T = | |
if typeof<'T> = typeof<string> then "String" :> obj | |
elif typeof<'T> = typeof<int> then 1 :> obj | |
else failwith "Unsupported type" | |
|> unbox<'T> | |
[<Fact>] | |
let ``Can Factory 5`` () = | |
let factory : Factory<_> = factoryFunction | |
// Create a string | |
let testString = factory() | |
Assert.Equal("String", testString) | |
// Create an int | |
let testInt = factory() | |
Assert.Equal(1, testInt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment