views:

200

answers:

3

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();
    }
+1  A: 

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())
kvb
+1  A: 

See also

http://stackoverflow.com/questions/844733/why-cant-fs-type-inference-handle-this

for some limitations here.

Brian
+1  A: 

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).

Tomas Petricek