gpt4 book ai didi

generics - 如何使用静态解析的类型参数解决递归映射中的奇怪类型错误?

转载 作者:行者123 更新时间:2023-12-04 03:56:47 25 4
gpt4 key购买 nike

type CudaInnerExpr<'t> = CudaInnerExpr of expr: string with
member t.Expr = t |> fun (CudaInnerExpr expr) -> expr

type CudaScalar<'t> = CudaScalar of name: string with
member t.Name = t |> fun (CudaScalar name) -> name

type CudaAr1D<'t> = CudaAr1D of CudaScalar<int> * name: string with
member t.Name = t |> fun (CudaAr1D (_, name)) -> name

type CudaAr2D<'t> = CudaAr2D of CudaScalar<int> * CudaScalar<int> * name: string with
member t.Name = t |> fun (CudaAr2D (_, _, name)) -> name

type ArgsPrinter = ArgsPrinter with
static member inline PrintArg(_: ArgsPrinter, t: CudaScalar<float32>) = sprintf "float %s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaScalar<int>) = sprintf "int %s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaAr1D<float32>) = sprintf "float *%s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaAr1D<int>) = sprintf "int *%s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<float32>) = sprintf "float *%s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<int>) = sprintf "int *%s" t.Name

static member inline PrintArg(_: ArgsPrinter, (x1, x2)) =
let inline print_arg x =
let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
call ArgsPrinter
[|print_arg x1;print_arg x2|] |> String.concat ", "
static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) =
let inline print_arg x =
let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
call ArgsPrinter
[|print_arg x1;print_arg x2;print_arg x3|] |> String.concat ", "

static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) =行中,表达式 (x1, x2, x3)给了我以下错误:
Script1.fsx(26,52): error FS0001: This expression was expected to have type
'in_
but here has type
'a * 'b * 'c

知道在这里做什么才能使这项工作吗?

最佳答案

在我看来,您想要执行以下操作:

    ...

static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<float32>) = sprintf "float *%s" t.Name
static member inline PrintArg(_: ArgsPrinter, t: CudaAr2D<int>) = sprintf "int *%s" t.Name

let inline print_arg x =
let inline call (tok : ^T) = ((^T or ^in_) : (static member PrintArg: ArgsPrinter * ^in_ -> string) tok, x)
call ArgsPrinter

type ArgsPrinter with
static member inline PrintArg(_: ArgsPrinter, (x1, x2)) = [|print_arg x1;print_arg x2|] |> String.concat ", "
static member inline PrintArg(_: ArgsPrinter, (x1, x2, x3)) = [|print_arg x1;print_arg x2;print_arg x3|] |> String.concat ", "

您可以在类型的中间定义泛型函数,因为您将在最后两个重载中使用该泛型函数,这将成为“递归重载”的一种。

请注意,此技术是 FSharpPlus当前使用的技术,实际上是该技术的简化。

最后请注意,您的解决方案对我来说似乎也是正确的(尽管更为冗长),但由于某些原因,F#编译器会感到困惑,我无法向您解释为什么,但是遇到了这种情况,而我所能做的就是找到最小限度的问题。解决方法,并将其报告给F#家伙。约束求解器中还有很多事情需要解决。

关于generics - 如何使用静态解析的类型参数解决递归映射中的奇怪类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41807147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com