我可以在f#中使用动态类型吗?

时间:2022-04-12 12:57:36

.net4.0

.net4.0

mytest.py

mytest.py

def Add (a, b):
    return a+b

I can use it in C# 4, like this:

我可以在c# 4中使用它,像这样:

        ScriptRuntime runtime = Python.CreateRuntime();
        dynamic script = runtime.UseFile("mytest.py");

        Console.WriteLine(script.Add(1, 3));

But, how can I use dynamic in F#?

但是,如何在f#中使用动态呢?

open System
open Microsoft.Scripting.Hosting
open IronPython.Hosting

let call a b=
    let runtime = Python.CreateRuntime()
    let script = runtime.UseFile("mytest.py")
    script.Add(a,b)

1 个解决方案

#1


3  

There is a module FSharp.Interop.Dynamic, on nuget that implements the dynamic operator using the dlr. So That:

有一个模块FSharp.Interop。动态的,在nuget上使用dlr实现动态操作。这样:

open System
open Microsoft.Scripting.Hosting
open IronPython.Hosting
open EkonBenefits.FSharp.Dynamic

let call a b=
    let runtime = Python.CreateRuntime()
    let script = runtime.UseFile("mytest.py")
    script?Add(a,b)

It has several advantages over a lot of the snippets out there.

它比外面的许多代码片段有几个优势。

  • Performance it uses Dynamitey for the dlr call and Dynamitey implements caching of the callsites and is a PCL library
  • 它使用Dynamitey进行dlr调用,Dynamitey实现了callsites的缓存,是一个PCL库。
  • Handles methods that return void, you'd get a binding exception if you didn't discard results of those when the binder was setup.
  • 处理返回void的方法,如果没有丢弃绑定器的结果,则会得到一个绑定异常。
  • The dlr handles the case of calling a delegate return by a function automatically, impromptu fsharp will also allow you to do the same with an FSharpFunc
  • dlr处理通过自动的函数调用委托返回的情况,即兴的fsharp也允许您使用FSharpFunc进行相同的操作。
  • Adds an !? prefix operator to handle invoking directly dynamic objects and functions you don't have the type at runtime.

    增加了一个! ?前缀运算符来处理直接调用动态对象和函数,而这些对象和函数在运行时没有类型。

    It's open source, Apache license, you can look at the implementation and it includes unit test example cases.

    它是开源的,Apache许可,你可以看看它的实现,它包括单元测试的例子。

#1


3  

There is a module FSharp.Interop.Dynamic, on nuget that implements the dynamic operator using the dlr. So That:

有一个模块FSharp.Interop。动态的,在nuget上使用dlr实现动态操作。这样:

open System
open Microsoft.Scripting.Hosting
open IronPython.Hosting
open EkonBenefits.FSharp.Dynamic

let call a b=
    let runtime = Python.CreateRuntime()
    let script = runtime.UseFile("mytest.py")
    script?Add(a,b)

It has several advantages over a lot of the snippets out there.

它比外面的许多代码片段有几个优势。

  • Performance it uses Dynamitey for the dlr call and Dynamitey implements caching of the callsites and is a PCL library
  • 它使用Dynamitey进行dlr调用,Dynamitey实现了callsites的缓存,是一个PCL库。
  • Handles methods that return void, you'd get a binding exception if you didn't discard results of those when the binder was setup.
  • 处理返回void的方法,如果没有丢弃绑定器的结果,则会得到一个绑定异常。
  • The dlr handles the case of calling a delegate return by a function automatically, impromptu fsharp will also allow you to do the same with an FSharpFunc
  • dlr处理通过自动的函数调用委托返回的情况,即兴的fsharp也允许您使用FSharpFunc进行相同的操作。
  • Adds an !? prefix operator to handle invoking directly dynamic objects and functions you don't have the type at runtime.

    增加了一个! ?前缀运算符来处理直接调用动态对象和函数,而这些对象和函数在运行时没有类型。

    It's open source, Apache license, you can look at the implementation and it includes unit test example cases.

    它是开源的,Apache许可,你可以看看它的实现,它包括单元测试的例子。