제약 조건

이 항목에서는 제네릭 형식 또는 함수에서 형식 인수에 대한 요구 사항을 지정하기 위해 제네릭 형식 매개 변수에 적용할 수 있는 제약 조건에 대해 설명합니다.

구문

type-parameter-list when constraint1 [ and constraint2]

설명

제네릭 형식에서 사용할 수 있는 형식을 제한하기 위해 적용할 수 있는 몇 가지 제약 조건이 있습니다. 다음 표에서는 이러한 제약 조건을 나열하고 설명합니다.

제약 조건 구문 설명
형식 제약 조건 type-parameter :>type 제공된 형식은 지정된 형식과 같거나 파생되어야 합니다. 또는 형식이 인터페이스인 경우 제공된 형식이 인터페이스를 구현해야 합니다.
Null 제약 조건 type-parameter : null 제공된 형식은 null 리터럴을 지원해야 합니다. 여기에는 F# 목록, 튜플, 함수, 클래스, 레코드 또는 공용 구조체 형식이 아닌 모든 .NET 개체 형식이 포함됩니다.
명시적 멤버 제약 조건 [(]type-parameter [또는 ... 또는 type-parameter)] : (member-signature) 제공된 형식 인수 중 하나 이상에는 지정된 서명이 있는 멤버가 있어야 합니다. 일반적인 용도로 사용되지 않습니다. 명시적 멤버 제약 조건의 유효한 대상이 되려면 멤버가 암시적 형식 확장의 형식 또는 일부에서 명시적으로 정의되어야 합니다.
생성자 제약 조건 type-parameter : ( new : unit -> 'a ) 제공된 형식에는 매개 변수가 없는 생성자가 있어야 합니다.
값 형식 제약 조건 type-parameter : 구조체 제공된 형식은 .NET 값 형식이어야 합니다.
참조 형식 제약 조건 type-parameter : 구조체 아님 제공된 형식은 .NET 참조 형식이어야 합니다.
열거형 형식 제약 조건 type-parameter : enum<underlying-type> 제공된 형식은 지정된 기본 형식이 있는 열거형 형식이어야 합니다. 일반적인 용도로 사용되지 않습니다.
대리자 제약 조건 type-parameter : delegate<tuple-parameter-type, return-type> 제공된 형식은 지정된 인수와 반환 값이 있는 대리자 형식이어야 합니다. 일반적인 용도로 사용되지 않습니다.
비교 제약 조건 type-parameter : comparison 제공된 형식은 비교를 지원해야 합니다.
같음 제약 조건 type-parameter : equality 제공된 형식은 같음을 지원해야 합니다.
관리되지 않는 제약 조건 type-parameter : 관리되지 않음 제공된 형식은 관리되지 않는 형식이어야 합니다. 관리되지 않는 형식은 특정 기본 형식(sbyte, ,byte, char, nativeint, unativeint, , int16float32uint32uint64int32uint16int64float또는 ), 열거형 형식 nativeptr<_>또는 decimal필드가 모두 관리되지 않는 형식인 제네릭이 아닌 구조체입니다.

코드가 제약 조건 형식에서 사용할 수 있지만 일반적인 형식에서는 사용할 수 없는 기능을 사용해야 하는 경우 제약 조건을 추가해야 합니다. 예를 들어 형식 제약 조건을 사용하여 클래스 형식을 지정하는 경우 제네릭 함수 또는 형식에서 해당 클래스의 메서드 중 하나를 사용할 수 있습니다.

형식 매개 변수를 명시적으로 작성할 때 제약 조건을 지정해야 하는 경우가 있습니다. 제약 조건이 없으면 컴파일러에서 사용 중인 기능을 런타임에 형식 매개 변수에 대해 제공할 수 있는 모든 형식에서 사용할 수 있는지 확인할 방법이 없기 때문입니다.

F# 코드에서 사용하는 가장 일반적인 제약 조건은 기본 클래스 또는 인터페이스를 지정하는 형식 제약 조건입니다. 다른 제약 조건은 F# 라이브러리에서 산술 연산자에 대한 연산자 오버로드를 구현하는 데 사용되는 명시적 멤버 제약 조건과 같은 특정 기능을 구현하는 데 사용되거나 F#이 공용 언어 런타임에서 지원하는 전체 제약 조건 집합을 지원하기 때문에 기본 제공됩니다.

형식 유추 프로세스 중에 일부 제약 조건은 컴파일러에 의해 자동으로 유추됩니다. 예를 들어 함수에서 연산자를 사용하는 + 경우 컴파일러는 식에 사용되는 변수 형식에 대해 명시적 멤버 제약 조건을 유추합니다.

다음 코드에서는 몇 가지 제약 조건 선언을 보여 줍니다.

// Base Type Constraint
type Class1<'T when 'T :> System.Exception> =
    class end

// Interface Type Constraint
type Class2<'T when 'T :> System.IComparable> =
    class end

// Null constraint
type Class3<'T when 'T : null> =
    class end

// Member constraint with instance member
type Class5<'T when 'T : (member Method1 : 'T -> int)> =
    class end

// Member constraint with property
type Class6<'T when 'T : (member Property1 : int)> =
    class end

// Constructor constraint
type Class7<'T when 'T : (new : unit -> 'T)>() =
    member val Field = new 'T()

// Reference type constraint
type Class8<'T when 'T : not struct> =
    class end

// Enumeration constraint with underlying value specified
type Class9<'T when 'T : enum<uint32>> =
    class end

// 'T must implement IComparable, or be an array type with comparable
// elements, or be System.IntPtr or System.UIntPtr. Also, 'T must not have
// the NoComparison attribute.
type Class10<'T when 'T : comparison> =
    class end

// 'T must support equality. This is true for any type that does not
// have the NoEquality attribute.
type Class11<'T when 'T : equality> =
    class end

type Class12<'T when 'T : delegate<obj * System.EventArgs, unit>> =
    class end

type Class13<'T when 'T : unmanaged> =
    class end

// Member constraints with two type parameters
// Most often used with static type parameters in inline functions
let inline add(value1 : ^T when ^T : (static member (+) : ^T * ^T -> ^T), value2: ^T) =
    value1 + value2

// ^T and ^U must support operator +
let inline heterogenousAdd(value1 : ^T when (^T or ^U) : (static member (+) : ^T * ^U -> ^T), value2 : ^U) =
    value1 + value2

// If there are multiple constraints, use the and keyword to separate them.
type Class14<'T,'U when 'T : equality and 'U : equality> =
    class end

참고 항목