One way is to use tuples:

1
2
3
4
5
6
7
type MyClass( ?optParam : SomeClass) = 
    let x1,y1 = 
        match optParam with
        | Some x -> x.MemberValue, x.OtherMemberValue
        | None -> 0, 0
    member c.X1 = x1
    member c.Y1 = y1
By on 1/3/2009 9:57 PM ()

I'm going with tuples. Now I have question about order of evaluation and initialization of values. I have something like this:

1
2
3
4
5
6
7
8
9
10
type MyClass( ?optParam : SomeClass) = 
    let x1,y1,z1 = 
        match optParam with
        | Some x -> 
            MyFunc1 x.MemberValue, 
            MyFunc2 x.OtherMemberValue, 
            x1/y1
        | None -> 0, 0
    member c.X1 = x1
    member c.Y1 = y1

Is there a syntax that allows me to use expression x1/y1 for third element in tuple?

Currently I wrote it like this:

1
2
3
4
5
6
7
8
9
10
type MyClass( ?optParam : SomeClass) = 
    let x1,y1,z1 = 
        match optParam with
        | Some x -> 
            let tempX1 = MyFunc1 x.MemberValue
            let tempY1 = MyFunc2 x.OtherMemberValue 
            tempX1, tempY1, tempX1 / tempY1
        | None -> 0, 0, 0
...

This works fine, it's short, but it would be nice if I can be written without temporary storage tempX1 and tempY1.
MyFunc2 will never return 0 as result, so this division is safe without checking.

By on 1/4/2009 5:12 AM ()

Hi Z,

There is a lot of repetition in your code. You don't like this - good. Now take the next step: encapsulate the repetitive part...in a function perhaps?

1
2
let defaultArg arg defaultValue = match arg with None -> defaultValue | Some v -> v

In your class:

1
2
let x1 = defaultArg optParam 0.0

Much better and clearer!

I didn't invent this, I think I learned it from a post here or blog or whatever.

Brian,

This function is imo useful, reusable and recurring enough to belong in FSharp.Core.

thanks,

Kurt

By on 1/4/2009 4:43 AM ()

@Kurt

It is part of FSharp.Core.Operators, you can try it out in fsi

[link:research.microsoft.com]

By on 1/4/2009 11:04 AM ()

@Kurt

It is part of FSharp.Core.Operators, you can try it out in fsi

[link:research.microsoft.com]

I'll be damned - it is :)

thanks!

Kurt

By on 1/4/2009 12:49 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper