Statically Resolved Type Parameters

前端 未结 2 742
一整个雨季
一整个雨季 2020-12-10 18:27

The following (simplified) snippet is taken from an application I\'m implementing which consistently uses Type Parameters resolved statically.

type A< ^B          


        
2条回答
  •  我在风中等你
    2020-12-10 18:57

    Your implementation @Tomas satisfies the question but there is some hesitation over the style of your solution because it does not respected the functional programming paradigm. I thought to a solution arising from the Type Classes implementation of Haskell. Interfaces, abstract classes, etc. have been implemented in order to allow the F# environment to “interact” with the .Net environment, for a reason of uniformity of style the use of code which implements interfaces, abstract classes etc. in a context where no need of interact with the .Net libraries, it is a “Overhead” in my opinion (although F# is a multi-paradigm language). This is the reason why I found the implementation of the Type Classes of Haskell very elegant. Below I implemented through F# the “Haskell Type Class” code to solve my problem.

    type Operations< ^a> = 
      {  
        MyMember : unit -> unit
      }
    
    type A< ^a> = {
      Operations : Operations< ^a>
    }
    
    and TestA = { 
      AField : A< BTy > 
    }
    
    and BTy = { 
      BField : Unit 
    }
    
    let it = 
      let BTy_operations : Operations< BTy > = { MyMember = fun () -> () }
      let A_of_BTy = { Operations = BTy_operations }
      { AField = A_of_BTy }
    

提交回复
热议问题