Kleisli合成的编译错误 [英] Compile error for Kleisli composition

查看:46
本文介绍了Kleisli合成的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从面向铁路的编程中复制了一个验证模块,该模块在我的应用程序中执行错误处理:

I have a validation module copied from Railway oriented programming that performs error handling in my application:

type ErrorMessage = ErrorMessage of string

type ValidationResult<'T> =
    | Success of 'T
    | Error of ErrorMessage

module ValidationResult =    
    let doubleMap successHandler errorHandler = function
        | Success x -> successHandler x
        | Error e -> errorHandler e

    let bind f = function
        | Success x -> f x
        | Error e -> Error e

    let (>=>) f g = f >> bind g

我正在使用以下测试功能来测试Kleisli成分:

I was testing the Kleisli composition by using the following test functions:

let validation1 (list: int list) =
    if List.length list = 6
    then Success list
    else Error <| ErrorMessage "Length error"

let validation2 list =
    if List.forall (fun x -> x > 6) list
    then Success list
    else Error <| ErrorMessage "All elements must be larger than 6"

let combined = validation1 >=> validation2
                              //^^^^^^^^^^^^ compile error

据我了解,validation1validation2应该组成,因为它们都是int list -> ValidationResult<int list>类型.但是我遇到了编译错误

To my understanding, validation1 and validation2 should compose because both are of type int list -> ValidationResult<int list>. However I got a compile error

期望一个支持运算符'> =>'的类型,但具有功能 类型.您可能缺少函数的参数.

Expecting a type supporting the operator '>=>' but given a function type. You may be missing an argument to a function.

我该如何解决?

推荐答案

似乎您只是忘记了open ValidationResult,所以您的合成运算符不在范围内.

It seems that you simply forgot to open ValidationResult, so your composition operator is not in scope.

对于正常功能,F#会抱怨该符号未定义.但是运营商是另一回事.

For a normal function, F# would have complained that the symbol was undefined. But operators are a different matter.

可以通过两种方式定义运算符:作为独立函数(功能方式)或作为传递给运算符的一种类型的静态成员(.NET方式).在前一种情况下,该函数必须在作用域中可见,而在后一种情况下则不可见:只要您设法通过定义为静态成员的运算符来控制对象,就不需要其类型可见.

Operators can be defined in two ways: as a standalone function (the functional way) or as a static member on one of the types passed to the operator (the .NET way). In the former case, the function needs to be visible in scope, but in the latter case it doesn't: as long as you managed to get hold of an object with an operator defined as static member, you don't need to have its type visible.

这就是为什么F#说它期望类型支持运算符"而不是函数未定义".

This is why F# says that it "expected a type supporting operator" instead of "function is undefined".

这篇关于Kleisli合成的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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