F#中的显式类型递归 [英] Explicit type recursion in F#

查看:123
本文介绍了F#中的显式类型递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自这个问题:

在F#中是否可以进行显式类型递归?

type 'a Mu = In of 'a Mu 'a

let unIn (In x) = x

不幸的是,这段代码给出了类型参数不能用作类型构造函数.

This code unfortunatly gives "Type parameter cannot be used as type constructor.

备注:本文函数式编程中使用了此构造例如,具有重载和高阶多态性.

用法示例(摘自此处):

type ('a, 'b) ListX =
    | Nil
    | Cons of 'a * 'b

type 'a List = ListX Mu

推荐答案

否,这是不可能的.具体而言,F#中的泛型与CLR具有相同的限制,即< T>或<'a>必须具有类型"*".同样的限制意味着您无法直接在F#中编写类型类",因为例如"Monad m"将采用种类较高的参数"m"(例如"*-> *",其中"list"和"option"可以是实例,它们各自都是泛型类型构造函数),但是不允许这样做

No, this is not possible. Specifically, generics in F# have the same limitation as the CLR, namely a <T> or an <'a> must have kind " * ". This same limitation is what means you cannot author "type classes" directly in F#, since e.g. "Monad m" would take a higher-kinded argument 'm' (e.g. "* -> *", where e.g. 'list' and 'option' could be instances, those each themselves being generic type constructors), but this is not allowed.

这篇关于F#中的显式类型递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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