是否可以使用管道运算符来调用返回对象上的方法?

发布于 2024-08-04 14:40:25 字数 746 浏览 7 评论 0原文

是否可以使用管道中缀运算符在返回的对象上调用方法?

例如,我有一个带有方法 (Method1) 的 .Net 类 (Class1)。我目前可以这样编码:

let myclass = new Class1()
let val = myclass.Method1()

我知道我也可以这样编码

let val = new Class1().Method1()

但是我希望能够对其进行管道化(我正在使用下面的?,我不知道该怎么做):

new Class1()
|> ?.Method1()

此外,说我有一个返回对象的方法,并且我只想在该方法不返回 null 时引用它(否则保释?)

new Class1()
|> ?.Method1()
|> ?? ?.Method2()

或者为了更清楚,这里是一些 C# 代码:

    public void foo()
    {
        var myclass = new Class1();
        Class2 class2 = myclass.Method1();
        if (class2 == null)
        {
            return;
        }
        class2.Method2();
    }

Is it possible to call a method on a returned object using the pipeline infix operator?

Example, I have a .Net class (Class1) with a method (Method1). I can currently code it like this:

let myclass = new Class1()
let val = myclass.Method1()

I know I could also code it as such

let val = new Class1().Method1()

However I would like to do be able to pipeline it (I am using the ? below where I don't know what to do):

new Class1()
|> ?.Method1()

Furthermore, say I had a method which returns an object, and I want to only reference it if that method didn't return null (otherwise bail?)

new Class1()
|> ?.Method1()
|> ?? ?.Method2()

Or to make it clearer, here is some C# code:

    public void foo()
    {
        var myclass = new Class1();
        Class2 class2 = myclass.Method1();
        if (class2 == null)
        {
            return;
        }
        class2.Method2();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-08-11 14:40:25

您可以相当轻松地定义类似于 (??) 运算符的内容(但运算符不能以问号开头):

let (~??) f x =
  if (x <> null) then
    f x

不幸的是,您的管道代码需要更详细一些(另外,请注意,您可以删除用于调用构造函数的 new 关键字):

Class1()
|> fun x -> x.Method1()

将它们放在一起:

Class1()
|> fun x -> x.Method1()
|> ~?? (fun x -> x.Method2())

You can define something similar to your (??) operator fairly easily (but operators can't start with a question mark):

let (~??) f x =
  if (x <> null) then
    f x

Unfortunately, your pipelined code will need to be a bit more verbose (also, note that you can drop the new keyword for calling constructors):

Class1()
|> fun x -> x.Method1()

Putting it all together:

Class1()
|> fun x -> x.Method1()
|> ~?? (fun x -> x.Method2())
老子叫无熙 2024-08-11 14:40:25

使用“kvb”建议的自定义运算符绝对是一种选择。在这种情况下,您可能会感兴趣的另一种方法是定义您自己的“计算表达式”,该表达式会在您指定的每个点自动执行 null 值检查。使用它的代码如下所示:

open System.Windows.Forms

// this function returns (0) null, or (1) btn whose parent is 
// null or (2) button whose parent is not null
let test = function
  | 1 -> new Button(Text = "Button")
  | 2 -> new Button(Text = "Button", Parent = new Button(Text = "Parent"))
  | _ -> null

let res =  
  safe { let! btn = test(2) // specify number here for testing
         // if btn = null, this part of the computation will not execute
         // and the computation expression immediately returns null
         printfn "Text = %s" btn.Text
         let! parent = btn.Parent // safe access to parent
         printfn "Parent = %s" parent.Text // will never be null!
         return parent }

如您所见,当您想要使用可能为“null”的值时,您可以在计算表达式中使用 let!。可以定义计算表达式,以便在值为 null 时立即返回 null,否则运行其余计算。这是代码:

type SafeNullBuilder() =
  member x.Return(v) = v
  member x.Bind(v, f) = 
    if v = null then null else f(v)

let safe = new SafeNullBuilder()    

顺便说一句:如果您想了解更多信息,它与 Haskell 中的“Maybe”monad(或使用 F# 选项类型的计算)非常相似。

Using a custom operator as 'kvb' suggests is definitely an option. Another approach that you may find interesting in this case is to define your own 'computation expression' that automatically performs the check for null value at every point you specify. The code that uses it would look like this:

open System.Windows.Forms

// this function returns (0) null, or (1) btn whose parent is 
// null or (2) button whose parent is not null
let test = function
  | 1 -> new Button(Text = "Button")
  | 2 -> new Button(Text = "Button", Parent = new Button(Text = "Parent"))
  | _ -> null

let res =  
  safe { let! btn = test(2) // specify number here for testing
         // if btn = null, this part of the computation will not execute
         // and the computation expression immediately returns null
         printfn "Text = %s" btn.Text
         let! parent = btn.Parent // safe access to parent
         printfn "Parent = %s" parent.Text // will never be null!
         return parent }

As you can see, when you want to use a value that can potentially be 'null', you use let! inside the computation expression. The computation expression can be defined so that it immediately returns null if the value is null and runs the rest of the computation otherwise. Here is the code:

type SafeNullBuilder() =
  member x.Return(v) = v
  member x.Bind(v, f) = 
    if v = null then null else f(v)

let safe = new SafeNullBuilder()    

BTW: If you want to learn more about this, it is very similar to 'Maybe' monad in Haskell (or computation working with F# option type).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文