F#中的OCaml样式通用类型参数的语法是什么? [英] What is the syntax for OCaml style generic type parameters in F#?

查看:82
本文介绍了F#中的OCaml样式通用类型参数的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此答案,F#支持OCaml样式类型参数.问题中的示例是:

According to this answer, F# supports OCaml style type parameters. The example in the question is:

type 'a NestedList = List of 'a NestedList list | Elem of 'a

但是,我在F#文档的任何地方都找不到该语法,而且我也无法让F#编译器接受我给出链接的答案中的语法. 编译器不接受使用多个参数的尝试:

However, I could not find this syntax documented anywhere in the F# documentation and moreover, I cannot get the F# compiler accept the syntax in the answer I gave the link to. This attempt to use multiple parameters are not accepted by the compiler:

type ('a * 'b) SomeType = ('a * 'b)

这有效:

type ('a , 'b) SomeType = ('a * 'b)
let x:SomeType<int,int> = (4,5)

基于Rider在x上方显示的类型注释,我假设这是公认的语法,但是我想知道它的记录位置以及是否正确.

Based on the type annotation Rider displays above x, I'm assuming this is the accepted syntax, but I'd like to know where this is documented and if I did get it right.

推荐答案

您是对的.您链接到的答案是错误的. type ('a * 'b) someType在OCaml中也无效.多个类型的参数也应使用逗号分隔:type ('a, 'b) someType.

You are right. The answer you link to is wrong. type ('a * 'b) someType is not valid in OCaml either. Multiple type parameters should be separated by comma there as well: type ('a, 'b) someType.

F#语言规范中指定了F#语言及其语法.有关类型的语法,请参见第5章(在4.1规范中),有关类型的定义,请参见第8章.

The F# language, including its syntax, is specified in The F# Language Specification. See in particular chapter 5 (in the 4.1 specification) for the syntax of types and chapter 8 for type definitions.

这篇关于F#中的OCaml样式通用类型参数的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆