F#类型提供程序:可以在定义它的同一项目中使用类型提供程序吗?

时间:2020-04-01 00:37:01

标签: .net-core f#

我遵循了有关F#类型提供程序there的教程,提供了源代码here

Program.fs

open System.Reflection
open Microsoft.FSharp.Core.CompilerServices
open ProviderImplementation.ProvidedTypes

[<TypeProvider>]
type StringTypeProvider(config: TypeProviderConfig) as this =
    inherit TypeProviderForNamespaces(config)

    let namespaceName = "Samples.StringTypeProvider"
    let thisAssembly = Assembly.GetExecutingAssembly()

    let staticParams = [ProvidedStaticParameter("value", typeof<string>)]

    let t = ProvidedTypeDefinition(thisAssembly, namespaceName, "StringTyped", Some typeof<obj>, hideObjectMethods = true)

    do t.DefineStaticParameters(
        parameters = staticParams,
        instantiationFunction = (fun typeName paramValues ->
            match paramValues with
            | [| :? string as value |] ->
                let ty = ProvidedTypeDefinition(
                            thisAssembly,
                            namespaceName,
                            typeName,
                            Some typeof<obj>
                        )

                let lengthProp = ProvidedProperty(
                                    "Length",
                                    typeof<int>,
                                    getterCode = fun _ -> <@@ value.Length @@>
                                )
                ty.AddMember lengthProp

                let charProps = value
                                    |> Seq.map(fun c ->
                                            let p = ProvidedProperty(
                                                        c.ToString(),
                                                        typeof<char>,
                                                        getterCode = fun _ -> <@@ c @@>
                                                    )
                                            let doc = sprintf "The char %s" (c.ToString())
                                            p.AddXmlDoc doc

                                            p
                                        )
                                    |> Seq.toList
                ty.AddMembersDelayed (fun () -> charProps)

                let sanitized = value.Replace(" ","")
                let valueProp = ProvidedProperty(
                                    sanitized,
                                    typeof<string>,
                                    getterCode = fun _ -> <@@ value @@>
                                )
                valueProp.AddXmlDoc "This is the value that you gave me to start with"
                ty.AddMember valueProp

                let ctor = ProvidedConstructor(
                            parameters = [],
                            invokeCode = fun args -> <@@ value :> obj @@>
                        )

                ctor.AddXmlDoc "Initializes a the awesomes"

                ty.AddMember ctor

                let reverser = ProvidedMethod(
                                    methodName = "Reverse",
                                    parameters = [],
                                    returnType = typeof<string>,
                                    invokeCode = (fun _ ->
                                                    <@@
                                                    value
                                                        |> Seq.map (fun x -> x.ToString())
                                                        |> Seq.toList
                                                        |> List.rev
                                                        |> List.reduce (fun acc el -> acc + el)
                                                    @@>))

                ty.AddMember reverser

                ty
            | _ -> failwith "No idea what you're doing"
        )
    )

    do this.AddNamespace(namespaceName, [t])
[<assembly:TypeProviderAssembly>]
do()

open Samples.StringTypeProvider

type snowman = Samples.StringTypeProvider.StringTyped< @"☃" >
let doYouWantToBuildASnowman = snowman()
doYouWantToBuildASnowman.``☃``

[<EntryPoint>]
let main _ =
    0

我有以下错误:

  <FSharpPlayground>\Program.fs:3819 The namespace or module 'Samples' is not defined.
  <FSharpPlayground>\Program.fs:3862 The namespace or module 'Samples' is not defined.
  <FSharpPlayground>\Program.fs:3975 The type 'Object' does not define the field, constructor or member '☃'.

哪个有意义,但是是否有一种解决方法可以使用在定义它的同一项目中定义的F#类型提供程序?

[编辑1]

即使我将整个StringTypeProvider定义放入带有namespace Samples.StringTypeProvider的另一个文件中,然后使用Program.fs导入到open Samples.StringTypeProvider中,也无法解决问题。

例如,输入type snowman = Samples.StringTypeProvider.StringTyped< @"☃" >,我得到:

Program.fs(3, 43): [FS0039] The type 'StringTyped' is not defined in 'Samples.StringTypeProvider'. Maybe you want one of the following:
   StringTypeProvider

[编辑2]

我尝试将类型提供程序定义和使用它的位置分开,但是我仍然遇到错误:https://github.com/ehouarn-perret/Issues.TypeProviders

0 个答案:

没有答案