Take the 2-minute tour ×

Hopefully this is coherent, albeit longwinded.

I am trying to create a property in one .fs file that I can set from a separate .fs file and then use that value in a module in the first .fs file... For instance,

In my first file function.fs, I would like to define a property theta.

I would then like to define a function Q in function.fs such that:

function Q = Q(r) and... Q(r) is dependent on some calculations that are dependent on theta,
A1(theta), A2(theta), A3(theta)

Q returns a data set in the form of a list.

I would also like to maintain a set of theta values in my main .fs file program.fs (i.e.
theta = [90;120;150;180])

I would then like to generate a data sets from function.fs for each theta.

My thought was to do this by setting a value for a property theta, running the program to generate a data set, setting a new value for theta, running the program to generate a data set, repeat... I have done a fair amount of research, what is not clear to me is how I actually recall the value of the property in the code for Q(r).

I have successfully setup a property in my function.fs file that I can set from program.fs:
In function.fs I have:

namespace models.test

type ContactAngle() =
    let mutable m_theta = 90.0
    //read only property
    member this.Empty = 
         m_theta = 90.0
     //read-write property
     //i think i'm onto something with this static...
     member this.Angle
         with get() =
         and set newAmt = 
             m_theta <- newAmt

//module HTModel =

And in program.fs I have:

open models.test

let me = new ContactAngle()
printfn "%A" me.Angle
me.Angle <- 120.0
printfn "%A" me.Angle

This allows me to redefine the value theta. Where I am struggling is how I now use the new property value in a function in function.fs.

I feel like I am missing something very elementary and need some help! Any insight would be greatly appreciated!

share|improve this question
I don't think this is very clear - are you asking about something like let rec ... and ... but going across files? –  John Palmer Oct 14 '12 at 8:25
I tried to understand but I got lost... if you rephrase and make it more simple, I'd be happy to advise though –  nicolas Oct 15 '12 at 11:01
There seems to be a basic misunderstanding : why do you talk about the location of your code ? in order to use code you need to call it, and the program does not care which file it is defined in. if you call it and it does not know where it is, it will bark at compilation. but after that it is his job... –  nicolas Oct 15 '12 at 12:37

1 Answer 1

Since functions evaluate when they are called not when created (exactly like in, for example, C#) you can just create a normal function in your ContactAngle type like this:

member this.DoSomenthingWithTheta multiplier
    m_theta <- m_theta * multiplier

You can reuse your mutable value anywhere in your class. To clarify all that you should read 'members' section of F# language reference.

But if you want to use that value outside your type and in a place where you don't have your initiated instance. Well then You would have to take a different approach. For example create a static mutable field and expose it with static property. Or create a singleton to store your value throughout the application.

But this kind of kills the 'spirit' of functional programming :).

share|improve this answer
Thanks for the feedback all. Your comments made me rethink what i was trying to do and it turns out the answer was over-complicate. If it's not obvious, I'm not a developer, but an engineering student trying to use F# to reproduce a model from a paper. Instead of rewriting my post I scrapped my first attempt, put it all in a single file and used recursion through multiple lists to accomplish my task, and it worked! Thanks for the help and the insight! –  mike Oct 20 '12 at 23:51

Your Answer


