gpt4 book ai didi

generics - 无法定义运算符

转载 作者:行者123 更新时间:2023-12-02 07:06:30 26 4
gpt4 key购买 nike

我有以下简单类型:

module Structures
type Point2D<'T> (x : 'T, y : 'T) =
member this.X = x
member this.Y = y
member this.IsEqualTo (p : Point2D<'T>) =
(this.X = p.X) && (this.Y = p.Y)

但 Visual Studio(2012 试用版)在名称“IsEqualTo”下放置了一条红色波浪线,在“this.X”下放置了一条蓝色波浪线。

红色波浪线的错误信息如下:

The generic member IsEqualTo has been used at a non-uniform instantiation prior to this program point. Consider reordering the members so this member occurs first. Alternatively, specify the full type of the members explicitly, including argument types, return types and any additional generic parameters and constraints.

蓝线的信息是:

A type parameter is missing a constraint 'when 'T : equality'

我试过将“IsEqualTo”放在所有其他成员之前,但错误仍然存​​在。

我猜这个错误与 this.X = p.X 中的 = 标志正在处理通用类型 的事实有关T 但我似乎无法更正它。

预先感谢您的帮助。

最佳答案

type Point2D<'T when 'T : equality> (x : 'T, y : 'T) =
member this.X = x
member this.Y = y
member this.IsEqualTo (p : Point2D<'T>) =
(this.X = p.X) && (this.Y = p.Y)

关于generics - 无法定义运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11069551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com