5.4.2 이항 연산자(Binary Operators)

5.4.2 이항 연산자(Binary Operators)

산술 연산자(Arithmetic operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
% modulo Float/Int Float/Int Float/Int
* multiplication Float/Int Float/Int Float/Int
/ division Float/Int Float/Int Float
+ addition Float/Int Float/Int Float/Int
- subtraction Float/Int Float/Int Float/Int

Float/Int 반환 타입에 대해: 피연산자 중 하나가 Float 타입이면 결과 표현식도 Float 타입이 되고, 그렇지 않으면 Int 타입이 됩니다. 나눗셈의 결과는 항상 Float예요. 정수 나눗셈(소수부를 버림)은 Std.int(a / b)를 사용하세요.

Haxe에서 modulo 연산의 결과는 제수가 음수가 아닐 때 항상 피제수(왼쪽 피연산자)의 부호를 유지해요. 제수가 음수일 때 결과는 대상(target)에 따라 다릅니다.

문자열 연결 연산자(String concatenation operator)

연산자 동작 피연산자 1 피연산자 2 결과 타입
+ concatenation any String String
+ concatenation String any String
+= concatenation String any String

"any" 피연산자는 문자열화(stringified)된다는 점을 주목하세요. 클래스와 abstract의 경우 문자열화는 사용자 정의 toString 함수로 제어할 수 있어요.

비트 연산자(Bitwise operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
<< shift left Int Int Int
>> shift right Int Int Int
>>> unsigned shift right Int Int Int
& bitwise and Int Int Int
` ` bitwise or Int Int
^ bitwise xor Int Int Int

논리 연산자(Logical operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
&& logical and Bool Bool Bool
` ` logical or Bool

단락 평가(Short-circuiting): Haxe는 같은 연산자를 가진 복합 불리언 표현식이 런타임에서 필요할 때까지만 왼쪽에서 오른쪽으로 평가됨을 보장해요. 예를 들어 A && B 표현식은 A를 먼저 평가하고, A의 평가가 true를 산출했을 때만 B를 평가합니다. 마찬가지로 A || B 표현식은 A의 평가가 true를 산출하면 B를 평가하지 않습니다. 그 경우엔 B의 값이 무관하기 때문입니다. 이는 다음과 같은 경우에 중요해요:

if (object != null && object.field == 1) { }

objectnull일 때 object.field에 접근하면 런타임 오류가 나지만, object != null 검사가 그것을 막아줍니다.

복합 할당 연산자(Compound assignment operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
%= modulo Float/Int Float/Int Float/Int
*= multiplication Float/Int Float/Int Float/Int
/= division Float Float/Int Float
+= addition Float/Int Float/Int Float/Int
-= subtraction Float/Int Float/Int Float/Int
<<= shift left Int Int Int
>>= shift right Int Int Int
>>>= unsigned shift right Int Int Int
&= bitwise and Int Int Int
` =` bitwise or Int Int
^= bitwise xor Int Int Int

모든 경우에 복합 할당은 주어진 변수·필드·구조체 멤버 등을 수정하므로 읽기 전용 값에는 동작하지 않아요. 복합 할당은 하위 표현식으로 사용될 때 수정된 값으로 평가됩니다:

var a = 3;
trace(a += 3); // 6
trace(a); // 6

/=의 첫 피연산자는 Haxe에서 나눗셈 결과가 항상 Float이므로 항상 Float여야 해요. 마찬가지로 +=-=는 두 번째 피연산자로 Float가 주어지면 첫 피연산자를 Int로 받을 수 없는데, 결과가 Float가 되기 때문입니다.

수치 비교 연산자(Numeric comparison operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
== equal Float/Int Float/Int Bool
!= not equal Float/Int Float/Int Bool
< less than Float/Int Float/Int Bool
<= less than or equal Float/Int Float/Int Bool
> greater than Float/Int Float/Int Bool
>= greater than or equal Float/Int Float/Int Bool

문자열 비교 연산자(String comparison operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
== equal String String Bool
!= not equal String String Bool
< lexicographically before String String Bool
<= lexicographically before or equal String String Bool
> lexicographically after String String Bool
>= lexicographically after or equal String String Bool

Haxe에서 String 타입의 두 값은 같은 길이와 같은 내용을 가질 때 동등하다고 간주됩니다:

var a = "foo";
var b = "bar";
var c = "foo";
trace(a == b); // false
trace(a == c); // true
trace(a == "foo"); // true

동등 연산자(Equality operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
== equal any any Bool
!= not equal any any Bool

피연산자 1과 피연산자 2의 타입은 유니파이되어야 해요.

열거(Enums): 매개변수가 없는 열거는 항상 같은 값을 나타내므로 MyEnum.A == MyEnum.A입니다. 매개변수가 있는 열거는 a.equals(b)로 비교할 수 있는데(Type.enumEq()의 줄임 표현이에요).

Dynamic: 적어도 한 피연산자가 Dynamic 타입인 비교는 지정되지 않으며 플랫폼별입니다.

기타 연산자(Miscellaneous operators)

연산자 동작 피연산자 1 피연산자 2 결과 타입
... interval(범위 반복 참조) Int Int IntIterator
=> arrow(맵, 키-값 반복, 맵 컴프리헨션 참조) any any -

출처: Binary Operators

본문

이항 연산자는 산술(사칙연산·modulo·나눗셈은 항상 Float), 문자열 연결(f의 any피연산자를 문자열화), 비트(쉬프트·and·or·xor), 논리(단락 평가 보장), 복합 할당, 수치·문자열 비교, 동등, 기타 연산자를 포괄합니다. 각 표는 연산자·동작·피연산자 타입·결과 타입을 보여줍니다.

핵심 규칙

  • 나눗셈 \/=는 항상 Float를 만듭니다.
  • 논리 연산자는 단락 평가돼 좌측이 결과를 결정하면 우측을 평가하지 않아요.
  • 문자열 동등은 길이와 내용이 같을 때 성립합니다.
  • 동등 비교의 피연산자는 유니파이되어야 해요.
  • 무인자 열거는 값으로, 인자 열거는 equals로 비교합니다.

더 알아보기