F# generating types in a type extension function within a type provider

笑着哭i 提交于 2019-12-13 14:23:28

问题


I have the following problem:

Within my type provider, I need to extend a generic type, that I have previously defined, with a method that will return an instance of this generic type. What I mean is, let's say we have :

type receiveType<'a> = class
   val Next:int
   val Type:string
   new (state,stateType) =
      {Next = state;Type = stateType;}                    
   end

later within my typeprovider I want to extend the type with the following function:

[<TypeProvider>] 
type ProviderTest() as this= (
   inherit TypeProviderForNamespaces() 
   let ns = "typeProviderTest.Provided"    
   let asm = Assembly.GetExecutingAssembly()

   let makeAType (s:string) =
      let typage =
         ProvidedTypeDefinition(asm, ns, s, Some typeof<obj>) 
      typage

   type receiveType<'a> with
      member this.receive(hey:'a,next:string)= 
         // generate a type from a string for instance
         // "hello" -> type helloType
         generateTypeFromString(next) 
        // let typage = ProvidedTypeDefinition(asm, ns, next, Some typeof<obj>)
        new receiveType<nextType>(5,"hello")   

The goal here for me is to generate an instance of the type receiveType<TypeHello> , iterate through a structure (Finite State Machine) to get the next possible message I can receive ( for instance "goodMorning"), create the type associated ( TypeGoodMorning ) and then generate an instance of receiveType<TypeGoodMorning>.

Thank you.

来源:https://stackoverflow.com/questions/37139024/f-generating-types-in-a-type-extension-function-within-a-type-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!