💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
Haxe 3 允许绑定函数通过部分参数应用。每个函数类型可以被认为是有一个bind字段,可以被调用,传递需要数量的参数来创建一个新的函数。下面展示: > Haxe 3 allows binding functions with partially applied arguments. Each function type can be considered to have a bind field, which can be called with the desired number of arguments in order to create a new function. This is demonstrated here: ~~~ class Main { static public function main() { var map = new haxe.ds.IntMap<String>(); var f = map.set.bind(_, "12"); $type(map.set); // Int -> String -> Void $type(f); // Int -> Void f(1); f(2); f(3); trace(map); // {1 => 12, 2 => 12, 3 => 12} } } ~~~ 第4行绑定函数 map.set 到一个变量 f ,然后应用 12 作为第二个参数。下划线用来表示这个参数没有绑定,通过对比map.set 和f类型展示:绑定的字符串参数被从类型中有效的切断,转换一个 Int->String->Void 类型为 Int->Void 。 > Line 4 binds the function map.set to a variable named f, and applies 12 as second argument. The underscore _ is used to denote that this argument is not bound, which is shown by comparing the types of map.set and f: The bound String argument is effectively cut from the type, turning a Int->String->Void type into Int->Void. 调用 f(1) 然后实际上调用了 map.set(1,"12"),调用 f(2) 和 f(3) 也是类似的。最后一行证明所有三个索引真实的映射到值 “12”。 > A call to f(1) then actually invokes map.set(1, "12"), the calls to f(2) and f(3) are analogous. The last line proves that all three indices indeed are mapped to the value "12". 下划线 \_ 可以被跳过对于后续的参数,所以第一个参数可以被使用 map.set.bind(1)绑定,生成一个 String->Void 函数,在调用中设置一个新的索引1的值。 > The underscore _ can be skipped for trailing arguments,so the first argument could be bound through map.set.bind(1),yielding a String->Void function that sets a new value for index 1 on invocation. **回调** >[warning] 花絮:回调 Haxe 3以前,曾经使用一个 callback关键字,可以被调用为一个函数参数然后后面跟任何数量的绑定参数。这个名字源于一个常见的用法,是一个回调函数被创建和 this 对象被绑定。回调只允许绑定为参数从左到右,因为不支持下划线。使用下划线的选择有一些争议和一些其他的建议,但是没有被认为是很优越的。毕竟,下划线至少看起来像是说“在这里填上值”,很好的描述了它的语义。 >[warning] **Trivia**: Callback Prior to Haxe 3, Haxe used to know a callback-keyword which could be called with a function argument followed by any number of binding arguments. The name originated from a common usage were a callback-function is created with the this-object being bound. Callback would allow binding of arguments only from left to right as there was no support for the underscore _. The choice to use an underscore was controversial and several other suggestions were made, none of which were considered superior. After all, the underscore _ at least looks like it’s saying “fill value in here”, which nicely describes its semantics.