我最初认为"as"和冒号运算符意味着完全相同的事情,为值或函数指定类型.但我确实发现了一个不一致的地方:
type Uppercase = string -> string
let uppercase:Uppercase = fun n ->
//code
Run Code Online (Sandbox Code Playgroud)
这很好用.但是如果我将冒号改为"as"
type Uppercase = string -> string
let uppercase as Uppercase = fun n ->
//code
Run Code Online (Sandbox Code Playgroud)
它打破了,说它不知道"n"是什么类型.当然,我可以通过这样做来解决这个问题
type Uppercase = string -> string
let uppercase as Uppercase = fun (n:string) ->
//code
Run Code Online (Sandbox Code Playgroud)
它再次开心.所以,我的问题是,为什么"as"与冒号不同,为什么看起来F#在使用"as"时不能进行类型推断?谢谢.
我现在已经在这几个小时了,看看我能做的每个网站和文档.我无法弄清楚如何从一个数组中删除一个,只有一个元素(在这种情况下,一个字符串),保持任何重复.
我确实找到了办法,但是,这绝对是残暴的:
let remItem gs item =
if (chkItem gs item) then
let mutable fr = [| |] //temporary array
let mutable don = false //check if we found the element
for i in gs.inventory do
if not (i = item) && don then
fr <- (Array.append fr [|i|])
//add to the temp array until we find our item
elif i = item && don = false then don <- true
//we found it, skip just once so …Run Code Online (Sandbox Code Playgroud) f# ×2