在F#中,如何将类型名称作为函数参数传递?

Shr*_*roy 3 parameters f# types

我想传递一个类型名称(例如int,或者string,甚至是用户定义类型的名称)作为函数参数.目前我正在做以下事情:

type IntegerOrIntegerList =
    | Integer of int
    | IntegerList of int list

let f (n : int) (d : 'T) : IntegerOrIntegerList =
    match (box d) with
    | :? int as i -> Integer(n)
    | _ -> IntegerList([0 .. n])
Run Code Online (Sandbox Code Playgroud)

但上述存在d是偶然的.表达上述逻辑的惯用F#方式是什么?

在此先感谢您的帮助.

Dan*_*iel 7

您可以使用类型arg:

let f<'T> (n : int) =
  if typeof<'T> = typeof<int> then Integer(n)
  else IntegerList([0 .. n])
Run Code Online (Sandbox Code Playgroud)