- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
let actionOne value =
sprintf "action one %s" (value.Substring(1))
let actionTwo (value:string) =
sprintf "action two: %s" (value.Substring(1))
let one = actionOne "one"
let two = actionTwo("two")
如何维护 Curried form并在 actionOne 函数上添加类型注释,value:string
?
[编辑]
MS 文档显示此示例以比较“元组形式”和“柯里化(Currying)形式”:
// Tuple form.
member this.SomeMethod(param1, param2) = ...
// Curried form.
let function1 param1 param2 = ...
我的代码无法编译,因为 value
需要一个 type 注释(因为它与 .Substring() 一起使用)。
我可以添加类型注释,但这会使 ActionOne 签名与 ActionTwo 相同。
这个改变迫使我在我想使用括号时使用它:
如何更改 ActionOne 函数(添加类型注释)并避免更改调用,即:保留没有括号?
最佳答案
正如我和 Bui 的评论所建议的那样,actionOne
和 actionTwo
都是柯里化(Currying)形式。但是因为只有一个参数,所以柯里化(Currying)形式和非柯里化(Currying)形式之间没有区别:另一种说法是参数是一个元组,最基本的元组。
如果您有两个或更多参数,它就会变得更相关。当您对参数进行元组时,它是元组形式,如果您不这样做,该函数将是可柯里化(Currying)的。
下面是如何分配或不分配类型注释:
/// curried form
let f x y = printfn "%A, %A" x y
/// tupled argument
let f (x, y) = printfn "%A, %A" x y
/// curried form with type annotations
let f (x: string) (y: string) = printfn "%A, %A" x y
/// tupled argument with type annotations
let f (x: string, y: string) = printfn "%A, %A" x y
/// curried form with type annotations and return type spec
let f (x: string) (y: string): unit = printfn "%A, %A" x y
/// tupled argument with type annotations and return type spec
let f (x: string, y: string): unit = printfn "%A, %A" x y
当我们在 FSI 中一一粘贴这些行时,您可以看到签名更改(最后两行除外,因为返回类型已经被推断为 unit
):
> let f x y = printfn "%A, %A" x y;;
val f : x:'a -> y:'b -> unit
> let f (x, y) = printfn "%A, %A" x y;;
val f : x:'a * y:'b -> unit
> let f (x: string) (y: string) = printfn "%A, %A" x y;;
val f : x:string -> y:string -> unit
> let f (x: string, y: string) = printfn "%A, %A" x y;;
val f : x:string * y:string -> unit
> let f (x: string) (y: string): unit = printfn "%A, %A" x y;;
val f : x:string -> y:string -> unit
> let f (x: string, y: string): unit = printfn "%A, %A" x y;;
val f : x:string * y:string -> unit
元组形式的另一种选择是有一个元组参数,这有时会派上用场。你像这样声明它,你可以像调用任何其他带有元组参数的函数一样调用它:或者使用一个已经是元组的变量,或者使用两个元组形式的参数:
// let type inference do the work for you:
> let f x = printfn "%A, %A" (fst x) (snd x);;
val f : 'a * 'b -> unit
// with specific type
> let f (x: string * string) = printfn "%A, %A" (fst x) (snd x);;
val f : string * string -> unit
在您的问题中回复您的编辑:
I can add the type annotation, but that makes ActionOne signature identical to ActionTwo. This change FORCE me to use the parenthesis when I want to use it:
parens 的使用似乎有些困惑。 MS 文档给出的示例建议“没有括号意味着没有元组”。这并不完全正确:
// no parens, curried
let f a b = a + b
let x = f 42 43
// with parens, this is identical and still curried
let f (a) (b) = a + b
let x = f (42) (43)
// with parens in other places, still identical, except for the type
let f (a) (b) = (a: int64) + (b: int64)
let x = f (42L: int64) (43L)
MS 文档显示括号的原因是您不能编写 f x, y = x + y
。在这种情况下,您必须使用括号。在 F# 的几乎所有情况下,这都与具有最高优先级的空格字符有关。逗号最低。因此,您必须给出括号以告诉编译器您希望参数被元组化:f (x, y) = x + y
。
在单参数函数的情况下,您需要带有类型注释的括号,否则您将指定返回类型。所以 f x: int = x + 12
是指定返回类型的函数,而 f (x: int) = x + 12
是指定返回类型的函数参数类型x
。
关于F# - 在 curried 函数参数上设置类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62245513/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!