简体   繁体   English

在F#中使用属性

[英]Using properties in F#

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, 我试图在一个.fs文件中创建一个属性,该属性可以从一个单独的.fs文件中设置,然后在第一个.fs文件的模块中使用该值...例如,

In my first file function.fs , I would like to define a property theta . 在我的第一个文件function.fs ,我想定义一个属性theta

I would then like to define a function Q in function.fs such that: 然后,我想在function.fs定义一个函数Q

function Q = Q(r) and... Q(r) is dependent on some calculations that are dependent on theta , function Q = Q(r)和... Q(r)取决于某些与theta有关的计算,
ie
A1(theta), A2(theta), A3(theta)

Q returns a data set in the form of a list. Q返回列表形式的数据集。

I would also like to maintain a set of theta values in my main .fs file program.fs (ie 我还想在我的主.fs文件program.fs维护一组theta值(即
theta = [90;120;150;180])

I would then like to generate a data sets from function.fs for each theta . 然后,我想从function.fs为每个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) . 我的想法是通过设置属性theta的值,运行程序以生成数据集,为theta设置新值,运行程序以生成数据集,重复……来做到这一点。从研究的角度来看,我不清楚我实际上是如何在代码Q(r)回想该属性的值的。

I have successfully setup a property in my function.fs file that I can set from program.fs : 我已经在我的function.fs文件中成功设置了可以从program.fs设置的属性:
In function.fs I have: function.fs我有:

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() =
             m_theta
         and set newAmt = 
             m_theta <- newAmt

//module HTModel =

And in program.fs I have: program.fs我有:

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 . 这使我可以重新定义值theta Where I am struggling is how I now use the new property value in a function in function.fs . 我正在苦苦挣扎的地方是我现在如何在function.fs中的函数中使用新的属性值。

I feel like I am missing something very elementary and need some help! 我觉得我缺少一些基本知识,需要一些帮助! Any insight would be greatly appreciated! 任何见解将不胜感激!

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: 由于函数会在不创建时就评估它们何时被调用(就像在C#中一样),因此您可以像这样在ContactAngle类型中创建一个普通函数:

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 . 为了阐明所有内容,您应该阅读F#语言参考的 “成员”部分。

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 :). 但这杀死了函数编程的“精神” :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM