用于分组常量的F#构造

时间:2011-02-03 20:37:51

标签: f# constants

我有几个带有标题的页面,我希望能够在某种枚举中将这些标题定义为字符串常量。像这样的东西

pageTitles =
| HOME = "Home"
| SALES = "Sales"
| MARKETING = "Marketing"
| LOGOUT = "Logout"

然后像:

一样使用它
if title = pageTitles.SALES then
   //Goto sales

2 个答案:

答案 0 :(得分:4)

结合Daniel / Tomas建议:

module PageTitles =
    [<Literal>]
    let HOME = "Home" 
    [<Literal>]
    let SALES = "Sales" 
...
open PageTitles
...
match title with
| HOME -> // goto home
| SALES -> // goto sales

答案 1 :(得分:3)

我认为你必须把它们放在一个模块中。例如:

module pageTitles =
  [<Literal>]
  let HOME = "Home"
  ...