Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created September 26, 2023 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/fa13caea79818e1a54b601e1137f3980 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/fa13caea79818e1a54b601e1137f3980 to your computer and use it in GitHub Desktop.
F# Factory With Type Inference
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