.Net 支持柯里化泛型吗?

发布于 2024-10-31 13:22:54 字数 374 浏览 0 评论 0原文

假设我们有一个嵌套泛型类:

public class A<T> {
    public class B<U> { }
}

这里,typeof(A.B<>)本质上是一个具有两个参数的泛型类,其中仅绑定第一个参数。

如果我有一个带有两个参数的类,

public class AB<T, U> { }

有没有办法引用“AB,其中 T=intU 保持打开状态”?如果不是,这是 C# 限制还是 CLR 限制?

Suppose we have a nested generic class:

public class A<T> {
    public class B<U> { }
}

Here, typeof(A<int>.B<>) is in essence a generic class with two parameters where only the first is bound.

If I have a single class with two parameters

public class AB<T, U> { }

Is there a way to refer to "AB with T=int and U staying open"? If not, is this a C# limitation, or a CLR limitation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓酥 2024-11-07 13:22:54

显然这不能在 C# 中完成,您必须指定两个类型参数,或者都不指定。

而且 CLR 似乎也不支持, A.B<>A.B<> 指的是相同类型:

Type t1 = typeof(A<int>).GetNestedType("B`1");
Type t2 = typeof(A<string>).GetNestedType("B`1");
// t1.Equals(t2) is true

两种类型的封闭类型是 A<> (开放泛型类型)

编辑:进一步测试表明 typeof(A.B) 实际上是 arity 2 的泛型类型,不是 arity 1 的嵌套泛型类型... typeof(A.B).GetGenericArguments() 返回一个带有 typeof(int) typeof(string) 的数组。因此 typeof(A.B<>) 实际上相当于 (AB),这是不支持的(泛型类型不能部分关闭)

Apparently it can't be done in C#, you have to specify either both type parameters, or none.

And it doesn't seem to be supported by the CLR either, A<int>.B<> and A<string>.B<> refer to the same type:

Type t1 = typeof(A<int>).GetNestedType("B`1");
Type t2 = typeof(A<string>).GetNestedType("B`1");
// t1.Equals(t2) is true

The enclosing type of both types is A<> (open generic type)

EDIT: further testing shows that typeof(A<int>.B<string>) is actually a generic type of arity 2, not a nested generic type of arity 1... typeof(A<int>.B<string>).GetGenericArguments() returns an array with typeof(int) and typeof(string). So typeof(A<int>.B<>) would actually be equivalent to (A.B)<int, >, which isn't supported (a generic type can't be partially closed)

忆依然 2024-11-07 13:22:54

这是你的想法吗?

   class AB<T, U>
   {
      protected T t;
      U u;
   }

   class C<U> : AB<int, U>
   {
      public void Foo()
      {
         t = 5;
      }
   }

Is this what you have in mind?

   class AB<T, U>
   {
      protected T t;
      U u;
   }

   class C<U> : AB<int, U>
   {
      public void Foo()
      {
         t = 5;
      }
   }
给妤﹃绝世温柔 2024-11-07 13:22:54

您可以使用 Func;泛型将多个参数作为返回类型的单个函数传递。柯里化了很多,将许多参数作为单个参数传递。

 [Fact]
    public async Task TestCurried()
    {
        Func<int, Func<int, int>> curried = x => y => x + y;
        int sum = curried(1)(3);
        Assert.Equal(sum.ToString(), "4");
    }
see
https://blog.tchatzigiannakis.com/generics-as-super-functions/

you can use the Func<int,int> generic to pass multiple parameters as a single function returning an type. Curried many to pass many arguments as a single parameter.

 [Fact]
    public async Task TestCurried()
    {
        Func<int, Func<int, int>> curried = x => y => x + y;
        int sum = curried(1)(3);
        Assert.Equal(sum.ToString(), "4");
    }
see
https://blog.tchatzigiannakis.com/generics-as-super-functions/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文