英文:
How to update a class with a record member using the Singleton Pattern in F#
问题
这是翻译好的部分:
假设我有以下的记录定义:
type WindowServices =
{
ReportMenu: Window option
ProgressNotes: Window option
PatientRegistration: Window option
}
此外,我有这个类,在其中我试图实现单例模式:
type WindowServiceManager private(wss) =
let mutable ws:WindowServices = wss
member this.WS
with get () = ws
and set (value) = ws <- value
static member val Instance =
let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}
WindowServiceManager(wss)
在类外部,单例 "Instance" 似乎可以通过以下方式正确获得:
let i = WindowServiceManager.Instance
然而,无论我尝试了什么,都不能改变默认记录值。也就是说,以下操作失败。在 WS 中从未进入新值:
let windowServices : WindowServices =
{ ReportMenu = Some reportMenu;
ProgressNotes = Some progressNoteWindow;
PatientRegistration = Some patientRegistrationWindow }
i.WS <- windowServices
如何完成这个操作?
TIA
附录:以下显示了失败。"failwith" 总是被触发:
let getReportMenuService : Window =
let i = WindowServiceManager.Instance
let p = match i.WS.ReportMenu with
| None -> failwith "[ReportMenuService] 出了点问题!"
| Some s -> s
p
英文:
Lets say I have this record definition:
type WindowServices =
{
ReportMenu: Window option
ProgressNotes: Window option
PatientRegistration: Window option
}
Furthermore, I have this class in which I am attempting to implement the Singleton pattern:
type WindowServiceManager private(wss) =
let mutable ws:WindowServices = wss
member this.WS
with get () = ws
and set (value) = ws <- value
static member val Instance =
let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}
WindowServiceManager(wss)
Outside of the class, the singleton "Instance" seems to correctly be obtained with:
let i = WindowServiceManager.Instance
However, absolutely nothing I have tried can change the default record values. That is, this fails. The new values in "windowServices" are never entered into WS:
let windowServices : WindowServices =
{ ReportMenu = Some reportMenu;
ProgressNotes = Some progressNoteWindow;
PatientRegistration = Some patientRegistrationWindow }
i.WS <- windowServices
How is this done?
TIA
Addendum: The following shows failure. "failwith" is ALWAYS hit:
let getReportMenuService : Window =
let i = WindowServiceManager.Instance
let p = match i.WS.ReportMenu with
| None -> failwith "[ReportMenuService] Something went wrong!"
| Some s -> s
p
答案1
得分: 1
以下是您的代码的翻译部分:
我认为在您的代码的其他地方可能发生了一些事情,您还没有与我们分享。您提供的代码按预期工作(使用Window
作为string
的别名以简化):
type Window = string
type WindowServices =
{
ReportMenu: Window option
ProgressNotes: Window option
PatientRegistration: Window option
}
type WindowServiceManager private(wss) =
let mutable ws:WindowServices = wss
member this.WS
with get () = ws
and set (value) = ws <- value
static member val Instance =
let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}
WindowServiceManager(wss)
let i = WindowServiceManager.Instance
let windowServices : WindowServices =
{ ReportMenu = Some "ReportMenu";
ProgressNotes = Some "ProgressNotes" ;
PatientRegistration = Some "PatientRegistration" }
i.WS <- windowServices
let getReportMenuService : Window =
let i = WindowServiceManager.Instance
let p =
match i.WS.ReportMenu with
| None -> failwith "[ReportMenuService] Something went wrong!"
| Some s -> s
p
getReportMenuService
|> printfn "%A" // "ReportMenu"
希望这对您有帮助。如果您需要进一步的帮助,请随时提问。
英文:
I think there's something going on elsewhere in your code that you haven't yet shared with us. The code you've provided works as expected (using Window
as an alias for string
for simplicity):
type Window = string
type WindowServices =
{
ReportMenu: Window option
ProgressNotes: Window option
PatientRegistration: Window option
}
type WindowServiceManager private(wss) =
let mutable ws:WindowServices = wss
member this.WS
with get () = ws
and set (value) = ws <- value
static member val Instance =
let wss = {ReportMenu = None; ProgressNotes = None; PatientRegistration = None}
WindowServiceManager(wss)
let i = WindowServiceManager.Instance
let windowServices : WindowServices =
{ ReportMenu = Some "ReportMenu";
ProgressNotes = Some "ProgressNotes" ;
PatientRegistration = Some "PatientRegistration" }
i.WS <- windowServices
let getReportMenuService : Window =
let i = WindowServiceManager.Instance
let p =
match i.WS.ReportMenu with
| None -> failwith "[ReportMenuService] Something went wrong!"
| Some s -> s
p
getReportMenuService
|> printfn "%A" // "ReportMenu"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论