【问题标题】:What is the `using` keyword in Haxe?Haxe 中的 `using` 关键字是什么?
【发布时间】:2015-06-19 08:30:09
【问题描述】:

我经常看到人们在他们的 Haxe 代码中使用关键字using。它似乎在import 声明之后。

例如,我发现这是一个代码sn -p:

import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
using haxe.macro.Tools;
using Lambda;

它有什么作用以及它是如何工作的?

【问题讨论】:

    标签: using haxe


    【解决方案1】:

    Haxe 的“使用”混合功能也称为“static extension”。这是 Haxe 的一个很好的语法糖特性;它们可以对代码的可读性产生积极影响。

    静态扩展允许在不修改其源代码的情况下对现有类型进行伪扩展。在 Haxe 中,这是通过声明一个带有 扩展类型的第一个参数的静态方法,然后通过 using 关键字将定义类引入上下文来实现的。

    看看这个例子:

    using Test.StringUtil;
    
    class Test {
        static public function main() {
            // now possible with because of the `using`
            trace("Haxe is great".getWordCount());
    
            // otherwise you had to type
            // trace(StringUtil.getWordCount("Haxe is great"));
        }
    }
    
    class StringUtil {
        public static inline function getWordCount(value:String) {
            return value.split(" ").length;
        }
    }
    

    在此处运行此示例:http://try.haxe.org/#C96B7

    Haxe 文档中的更多内容:

    【讨论】:

    • 使用“使用”比“导入”更快吗?
    • @c.c.它们是完全不同的东西
    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2022-12-05
    • 2011-01-31
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多