确保 SML 中的特定类型结果
我试图创建一个返回“point”类型元素的函数:
type point = {x : int, y : int};
fun pointadd (p1: point, p2: point) = (((#x p1) + (#x p2)), ((#y p1) + (#y p2)));
但是 SMLNJ 似乎没有为了理解我的意图,结果也应该是“point”类型:
use "test1.sml";
[opening test1.sml]
type point = {x:int, y:int}
val pointadd = fn : point * point -> int * int
Im trying to make a function that will return an element of type "point":
type point = {x : int, y : int};
fun pointadd (p1: point, p2: point) = (((#x p1) + (#x p2)), ((#y p1) + (#y p2)));
but SMLNJ doesn't seem to understand my intention that the result should be of type "point" as well:
use "test1.sml";
[opening test1.sml]
type point = {x:int, y:int}
val pointadd = fn : point * point -> int * int
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
point
是一种记录类型,但您返回的是一个元组。像这样的事情怎么样:
您可以在返回类型上添加类型保护以使类型更好,但它是等效的:
point
is a record type, but you are returning a tuple instead.How about something like this:
You can add a type guard on the return type to make the type nicer, but it's equivalent:
自从我使用 SML 以来已经有一段时间了,但是类型系统在打印类型签名时不会自动解析定义的类型。 你可以尝试这样的事情:
It's been quite a while since my SML days but afair the type system does not resolve defined types automatically when printing the type signature. You could try something like this: