我正在使用F#开发Web应用程序.考虑保护用户输入字符串免受SQL,XSS和其他漏洞的影响.
换句话说,我需要一些编译时约束,这些约束允许我将纯字符串与表示SQL,URL,XSS,XHTML等的字符串区分开来.
许多语言都有它,例如Ruby的原生字符串插值功能#{...}.
使用F#,似乎度量单位表现非常好,但它们仅适用于数字类型.
有几种解决方案采用运行时 UoM (链接),但我认为这是我的目标的开销.
我查看了FSharpPowerPack,似乎很有可能为字符串提出类似的东西:
[<MeasureAnnotatedAbbreviation>] type string<[<Measure>] 'u> = string
// Similarly to Core.LanguagePrimitives.IntrinsicFunctions.retype
[<NoDynamicInvocation>]
let inline retype (x:'T) : 'U = (# "" x : 'U #)
let StringWithMeasure (s: string) : string<'u> = retype s
[<Measure>] type plain
let fromPlain (s: string<plain>) : string =
// of course, this one should be implemented properly
// by invalidating special characters and then assigning a proper UoM
retype s
// Supposedly populated …Run Code Online (Sandbox Code Playgroud)