这个问题似乎很愚蠢,但我真的不明白。
module Responses =
type Failure =
| Problem of string
| Error of exn
| Timeout
static member toString x =
match x with
| Problem str -> sprintf "Problem %s" str
| Error e -> sprintf "Error %s" (e.ToString())
| Timeout -> "Timeout"
override x.ToString() = Failure.toString x
错误是
override x.ToString() = Failure.toString x;;
--------------------------------------^^^^^^^^
stdin(11,41): error FS0039: The field, constructor or member 'toString' is not defined
原因是 f# 出于某种原因认为那Failure
是类型Microsoft.FSharp.Core.Operations.Failure
当我尝试写作时
override x.ToString() = Responses.Failure.toString x
我明白了
Startup.fs(14,33): error FS0039: The namespace or module 'Responses' is not defined
当我重命名Failure
为 examplexFailure
时,它可以工作。但我真的不想重命名它。我可以以某种方式避免重命名并使用静态方法吗?