我需要IMessage接口.在C#我可以写
CDO.Message oMsg = new CDO.Message(); // ok
Run Code Online (Sandbox Code Playgroud)
但是当我尝试在F#中这样做时
let oMsg = new CDO.Message()
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
'new'不能用于接口类型.考虑使用对象表达式'{new ... with ...}'.
如何解决这个问题呢?
下午好.假设我有一个带有单个字母的utf-8文件,比如"f"(没有\n和空格),我试着获得一系列行长度.
(with-open [rdr (reader "test.txt")]
(doall (map #(.length %) (line-seq rdr))))
Run Code Online (Sandbox Code Playgroud)
我明白了
=> (2)
Run Code Online (Sandbox Code Playgroud)
为什么?是否有任何优雅的方法来获得第一个字符串的正确长度?
动态获取Option类型存在一些困难.假设我有一个功能:
let printType x =
if (box x) = null then printfn "the type is 'null'"
else printfn "the type is %A" (x.GetType())
Run Code Online (Sandbox Code Playgroud)
我们在这里有输出:
printType 3 // the type is System.Int32
printType (Some(3)) // the type is Microsoft.FSharp.Core.FSharpOption`1[System.Int32]
printType None // the type is null
printType null // the type is null
Run Code Online (Sandbox Code Playgroud)
获取表达式的类型时,如何区分None和null?