gpt4 book ai didi

haxe - 如何使用具有多个枚举值的开关?

转载 作者:行者123 更新时间:2023-12-05 00:45:51 26 4
gpt4 key购买 nike

我正在尝试这段代码:

enum SideType
{
Vex;
Cav;
Plano;
}

function drawLense(aLeftType:SideType){

switch (aLeftType)
{
case Cav:
leftCenter = -aRadius - d * 0.5;

case Vex:
leftCenter = -Math.cos(( -90 + offset) * Math.PI / 180) * aRadius-d*0.5;

case Plano:return ;
case Cav, Vex:
points1= drawCurve(1, -90 + offset + trim, 180 - offset * 2 - (trim * 2), leftCenter, aRadius);
_LB = points1[0];
_LA = points1[1];

}
}

但是编译的时候报错:

characters 8-16 : This pattern is unused

因此,它指向案例 Cav,Vex:

在上述情况下如何检查 Cav 或 Vex?

编辑

我发现如果我删除 case Cav & Case Vex,那么 case Cav, Vex 会起作用,但这不是我想要的,我不能在 or experision 中重复模式使用吗?像(案例 Cav||Vex)?

case (Cav || Vex) 会导致:

src/com/optics/components/Lense.hx:343: characters 8-38 : Case expression must be a constant value or a pattern, not an arbitrary expression

最佳答案

aLeftType 的值只有 3 个选择,VexCavPlano

var aLeftType = Vex;
switch (aLeftType)
{
case Cav:
// If aLeftType is `Cav`, run this line.
case Vex:
// If aLeftType is `Vex`, run this line.
case Plano:
// If aLeftType is `Plano`, run this line.
case Cav, Vex:
// If aLeftType is `Vex` or `Plano`, run this line...
// But the first 2 cases already covered `Vex` and `Plano`,
// so it will never be reached.
}

所以真的,第 4 种情况的代码永远不会运行。它类似于:

if (a == 1) {
trace("a is 1");
} else if (a == 1) {
trace("a is really 1"); // This can never be reached.
}

这意味着,你必须重新考虑你是否真的想做。

关于haxe - 如何使用具有多个枚举值的开关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com