F#: Casting to generic type

▼魔方 西西 提交于 2019-12-24 02:00:57

问题


I'm fairly new to F# and coming from a C++ background. I am trying to write a simple vector class which can be of generic type (int, float, etc) but I run into trouble with the default constructor. I want to initialize the values to be zero but to do this I need to somehow cast a concrete zero to a generic type but I'm not sure how to do this.

Perhaps some code might help. Here's what I have so far:

type Vector3D<'T> (x :'T, y: 'T, z: 'T) = 
    member this.x = x
    member this.y = y
    member this.z = z

    new() = Vector3D<'T>(0,0,0) // what goes here?

I have tried many things on the highlighted line but cannot seem to get the compiler to be happy. I tried, for instance, Vector3D('T 0, 'T 0, 'T 0) which I thought should cast the int zero to 'T zero but that did not work.

Am I missing something fundamental or is it merely a case of getting the right syntax?


回答1:


Here's a solution which uses the built-in generic zero function:

type Vector3D<'T> (x : 'T, y: 'T, z: 'T) =
    member this.x = x
    member this.y = y
    member this.z = z

let inline newVector () : Vector3D<_> =
    let zero = Core.LanguagePrimitives.GenericZero
    Vector3D(zero, zero, zero)

let v1 : Vector3D<int> = newVector ()
let v2 : Vector3D<double> = newVector ()
let v3 : Vector3D<int64> = newVector ()



回答2:


Try using the defaultof function:

type Vector3D<'T> (x :'T, y: 'T, z: 'T) = 
    member this.x = x
    member this.y = y
    member this.z = z

    new() = Vector3D<'T>(Unchecked.defaultof<'T>, 
                         Unchecked.defaultof<'T>, 
                         Unchecked.defaultof<'T>) 

Note that if 'T is a reference type, defaultof<'T> will be null. To get around this, you can use a generic type constraint to limit 'T to value types—also known as struct's.

type Vector3D<'T when 'T : struct> (x :'T, y: 'T, z: 'T) = 
    ...

With this, you will still be able to use this Vector3D<'T> with int, float, decimal, and many other commonly used types, but it will guarantee that none of the x, y, or z, members may be null.



来源:https://stackoverflow.com/questions/19905669/f-casting-to-generic-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!