Inferring generic types with functional composition

前端 未结 4 1591
猫巷女王i
猫巷女王i 2021-01-05 18:04

Suppose I want to implement a functional composition, like this:

    public Func Compose(Func f, Func g)
    {
            


        
4条回答
  •  走了就别回头了
    2021-01-05 18:31

    The following code would work:

    Func toUpper = ToUpper;
    Func replicate = Replicate;
    
    // now the compiler knows that the parameters are Func
    var h = Compose(toUpper, replicate);
    

    So maybe you can still get the readability improvement you are seeking by defining those variables only once and them reusing them throughout your tests (I'm assuming this is a test utility...)

提交回复
热议问题