참고 Tuple (C 스타일 구조체처럼 여러 유형이 있지만 이름이없는 유형)은 묻지 않는 것 같지만 Tagged Union/Sum type입니다.
튜플에는 각 유형 중 하나가 있으며 태그가 지정된 공용 구조는 2 개 이상의 유형 중 하나의 값만 보유합니다.
그가 준 예제는 C# enum (기본적으로 숫자의 이름)과 같은 haxe enum입니다. 참고
http://haxe.org/ref/enums에서 : Haxe 열거 형은 매우 유사하게 동작 주어진 방법 결과의 모든 "사건"의 사양 및 캡처를 가능하게 노동 조합을 태그합니다. 모든 Enum 상태는 switch 문에 지정해야하므로 주어진 메서드의 동작을 완전히 정의하고 이러한 동작을 처리하는 데 중요합니다.
모든 케이스가 항상 처리되도록하려면 아래 클래스에서 단일 액션 실행 메소드를 구현하지 마십시오.또한
당신이 어느 같은 이름 중 하나를 방지 할 수있는 오류 인 또는 오른쪽으로 결과되는 왼쪽 자주 (오류 또는 결과의 조합에 대한 특히을 사용한다.
(왼쪽/오른쪽으로)
여기이 회원 조합 중 하나를 구현 한 것입니다.
당신은 또한 아래 하나의 패턴에 같은 이름
public sealed class Union<A,B,C> : IEquatable<Union<A,B>>
public sealed class Union<A,B,C,D> : IEquatable<Union<A,B,C,D>>
public sealed class Union<A,B,C,D,E> : IEquatable<Union<A,B,C,D,E>>
와 3,4,5 회원 조합이의 사본을 할 수 있습니다
[Serializable()]
public sealed class Union<A,B> : IEquatable<Union<A,B>>
{
private enum TypeTag{ A, B};
private Union()
{
}
private A AValue;
private B BValue;
private TypeTag tag;
public static Union<A,B> CreateA (A a)
{
var u = new Union<A,B>();
u.AValue = a;
u.tag = TypeTag.A;
return u;
}
public static Union<A,B> CreateB (B b)
{
var u = new Union<A,B>();
u.BValue = b;
u.tag = TypeTag.B;
return u;
}
public U SelectOn<U> (Func<A, U> withA, Func<B, U> withB)
{
if (withA == null)
throw new ArgumentNullException ("withA");
if (withB == null)
throw new ArgumentNullException ("withB");
if (tag == TypeTag.A)
{
return withA (AValue);
}
else if (tag == TypeTag.B)
{
return withB (BValue);
}
throw new InvalidOperationException ("Unreachable code.");
}
public void Run (Action<A> actionIfA, Action<B> actionIfB)
{
if (actionIfA == null)
throw new ArgumentNullException ("actionIfA");
if (actionIfB == null)
throw new ArgumentNullException ("actionIfB");
if (tag == TypeTag.A)
{
actionIfA (AValue);
}
else if (tag == TypeTag.B)
{
actionIfB (BValue);
}
}
public void Run (Action<A> actionIfA)
{
if (actionIfA == null)
throw new ArgumentNullException ("actionIfA");
if (tag == TypeTag.A)
{
actionIfA (AValue);
}
}
public void Run (Action<B> actionIfB)
{
if (actionIfB == null)
throw new ArgumentNullException ("actionIfB");
if (tag == TypeTag.B) {
actionIfB (BValue);
}
}
public override string ToString()
{
if (tag == TypeTag.A)
{
return "Type A" + typeof(A).ToString() + ": " + AValue.ToString();
}
else if (tag == TypeTag.B) {
return "Type B" + typeof(B).ToString() + ": " + BValue.ToString();
}
throw new InvalidOperationException ("Unreachable code.");
}
public override int GetHashCode()
{
unchecked
{
int result = tag.GetHashCode();
if (tag == TypeTag.A) {
result = (result * 397)^(AValue != null ? AValue.GetHashCode() : 0);
} else if (tag == TypeTag.B) {
result = (result * 397)^(BValue != null ? BValue.GetHashCode() : 0);
}
return result;
}
}
public override bool Equals (object other)
{
if (other is Union<A,B>)
{
return this.Equals((Union<A,B>)other);
}
return false;
}
public bool Equals (Union<A,B> other)
{
if (this.tag != other.tag)
{
return false;
}
if (tag == TypeTag.A)
{
return this.AValue.Equals(other.AValue);
}
else if (tag == TypeTag.B)
{
return this.AValue.Equals(other.AValue);
}
return false;
}
}
사용 예 : 좀 더 필수/약간 빠른/덜 안전한 방법을 사용하려는 경우
var i = Union<int,string>.CreateA(5);
var s = Union<int,string>.CreateB("Fre");
s.Run(actionIfA: n => { Console.WriteLine("1.number*3 is " + n*3); },
actionIfB: str => { Console.WriteLine("1.uppercase string is " + str.ToUpper()); });
var r = i.SelectOn(withA: n => "2.number*3 is" + n*3 , withB: str=> "2.uppercase string is" + str.ToUpper());
Console.WriteLine(r);
s.Run(actionIfA: n => { Console.WriteLine("3. number*3 is " + n*3); });
s.Run(actionIfB: str => { Console.WriteLine("4. uppercase string is " + str.ToUpper()); });
i.Run(actionIfA: n => { Console.WriteLine("5. number*3 is " + n*3); });
Console.WriteLine("does i equals s:" + i.Equals(s));
이 태그를 만들어 공공 값 (또는 태그가 공개하고 가져 오기를 사용 (잘못된 방식으로 사용하는 경우 더 좋은 예외를 허용하는 OrThrow 메서드) if/else를 사용하거나 AValue/BValue (/ CValue/DValue/EValue를 사용하려면이 메서드를 켜십시오. 3/4/5 튜플). 이것은 모든 상태를 처리하도록 강제하는 것을 불가능하게 만들고, 엉망이되어 잘못된 유형을 사용하게 만듭니다.
'Tuple'또는'Tuple '과 다른 점은 무엇입니까? 샘플 사용법은 목표를 명확히 할 수 있습니다 ... –
달성하고자하는 것에 대해 좀 더 명확히 할 수 있습니까? 이 의도 된 디자인의 이유를 이해하지 못합니다. – PMF
아마, 사전이 당신이 찾고있는 것입니다. –