简体   繁体   English

在Haxe中,如何在函数中传递Enum值,然后在函数内将它们转换为String?

[英]In Haxe, how do you pass Enum values in functions, and then convert them to Strings within the function?

I can't seem to get this working, but I'd be surprised if it wasn't possible in Haxe. 我似乎无法正常工作,但是如果在Haxe中无法做到这一点,我会感到惊讶。

I'm trying to pass a couple of Enum values defined in my game to a function, so that it can then concatenate them as String types and pass that to other functions. 我试图将我在游戏中定义的几个Enum值传递给一个函数,以便它可以将它们串联为String类型,然后将其传递给其他函数。

Example: 例:

// In a general Entity class:
public override function kill():Void {
    messages.dispatchCombined(entityType, ListMessages.KILLED);

    super.kill();
}

And in my Messages.hx class: 在我的Messages.hx类中:

package common;

import msignal.Signal.Signal1;

/**
 * A Message / Event class using Signals bound to String names.
 * @author Pierre Chamberlain
 */
class Messages{
    var _messages:MessagesDef;

    public function new() {
        _messages = new MessagesDef();
    }

    public function add(pType:String, pCallback:FuncDef) {
        if (_messages[pType] == null) {
            _messages[pType] = new Signal1<Dynamic>();
        }

        var signals = _messages[pType];
        signals.add( pCallback );
    }

    public function dispatch(pType:String, pArg:Dynamic):Bool {
        var signals = _messages[pType];
        if (signals == null) return false;
        signals.dispatch(pArg);
        return true;
    }

    //Compiler doesn't like passing enums :(
    public inline function addCombined(pSource:Enum, pEvent:Enum, pCallback:FuncDef) {
        add( combine(pSource, pEvent), pCallback );
    }

    public inline function dispatchCombined(pSource:Enum, pEvent:Enum, pArg:Dynamic):Bool {
        return dispatch( combine(pSource, pEvent), pArg);
    }

    //How can I just pass the enum "names" as strings?
    static inline function combine(a:Enum, b:Enum):String {
        return String(a) + ":" + String(b);
    }
}


typedef MessagesDef = Map<String, Signal1<Dynamic>>;
typedef FuncDef = Dynamic->Void;

Note how addCombined , dispatchCombined and combine expect an "Enum" type, but in this case I'm not sure if Haxe actually expects the entire Enum "class" to be passed (ie: ListMessages instead of ListMessages.KILLED ) or if a value should work. 注意如何addCombineddispatchCombinedcombine预期的“枚举”类型,但在这种情况下,我不知道如果HAXE实际上预计整个枚举“类”传递(即: ListMessages代替ListMessages.KILLED ),或者如果一个值应该管用。 Anyways, compiler doesn't like it - so I'm assuming another special Type has to be used. 无论如何,编译器不喜欢它-所以我假设必须使用另一个特殊的Type。

Is there another way to go about passing enums and resolving them to strings? 还有另一种方法来传递枚举并将它们解析为字符串吗?

I think you need EnumValue as parameter type (if it is only for enum values), and use Std.String to convert to String values. 我认为您需要EnumValue作为参数类型(如果仅用于枚举值),并使用Std.String转换为String值。

static inline function combine(a:EnumValue, b:EnumValue):String {
    return Std.string(a) + ":" + Std.string(b);
}

Of course that can be written smaller using String interpolation : 当然可以使用String插值将其写得更小:

static inline function combine(a:EnumValue, b:EnumValue):String {
    return '$a:$b';
}

Of course that can be 'more dynamic' using type parameters : 当然,使用类型参数可以“更加动态”:

static inline function combine<A, B>(a:A, b:B):String {
    return '$a:$b';
}

There is totally no need to use Dynamic as suggested. 完全没有必要按照建议使用动态。 If you use Dynamic, you basically turn off the type system . 如果使用动态,则基本上可以关闭类型系统

live example: 现场示例:
http://try.haxe.org/#a8844 http://try.haxe.org/#a8844

Use Dynamic instead of Enum or pass them as Strings right away since you can always convert to enum from String if you need it later. 使用Dynamic而不是Enum或立即将它们作为String传递,因为如果以后需要它可以始终从String转换为枚举。

Anyway pass the enum as enum:Dynamic and then call Std.string(enum); 无论如何将枚举作为enum:Dynamic传递,然后调用Std.string(enum);。

EDIT: Using EnumValue is definitely better approach than Dynamic, I use Dynamic in these functions because I send more than just Enums there and I am not worried about type safety in that case. 编辑:使用EnumValue绝对比Dynamic更好,我在这些函数中使用Dynamic,因为我在那里发送的不仅仅是Enums,在那种情况下我也不担心类型安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM