简体   繁体   中英

Using record type for program parameters

Say I have a set of program parameters in a record type like this:

type Parameters = {Name : string; Number : int ...}

Also, say that I initially populate it from CLI parameters or a DB call or anything, really.

Later in the program, additional information is found that I want to add to the Parameters. My question is, can I (should I? what is the best practice) define my Parameters with a TBD parameter of some type. eg

type Parameters {...;TBD : Dictionary<string, string> }

so that when I start up, I can fill in the parameters I have (first set), and later, when the TBD thing becomes known, add that to the paramemeters?

Does that make sense? Is this the right way or is there a better approach I'm totally missing?

I think you should start by doing stuff in the simplest way possible, which in this case I am sure means just keep on using a record with plain fields, like the first source line in your question. In F#, this will get you a long way with little effort. If at any point the configuration gets humongous, and you feel a need for something more advanced to be able to cope with it, then you can consider changes.

Generally Discriminated Unions work well for parameters/arguments. Take a look at Argu or CommandLine (even if just for the design, I'm not suggesting it fits your needs). This assumes you can define your parameter space in advance and it remains fixed.

If you don't know the parameters in advance you can still define a default record, and just update the fields with with as they become available. If necessary you can use Some/None there. However if you don't even know the argument name (ie it would be a new field in the record), I would go for a dictionary/map itself, as you keep adding new key-value entries, which are what arguments are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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