다음을 통해 공유


인덱싱된 속성

정렬된 데이터에 대해 추상화되는 클래스를 정의할 때 기본 구현을 노출하지 않고 해당 데이터에 대한 인덱싱된 액세스를 제공하는 것이 도움이 될 수 있습니다. 이 작업은 멤버를 사용하여 수행됩니다 Item .

구문

식 구문:

// Looking up an indexed property
expr[idx]

/// Assign to an indexed property
expr[idx] <- elementExpr

멤버 선언에 대한 구문:

// Indexed property that can be read and written to
member self-identifier.Item
    with get(index-values) =
        get-member-body
    and set index-values values-to-set =
        set-member-body

// Indexed property can only be read
member self-identifier.Item
    with get(index-values) =
        get-member-body

// Indexed property that can only be set
member self-identifier.Item
    with set index-values values-to-set =
        set-member-body

설명

이전 구문의 형태는 a getset 메서드가 모두 있거나, 메서드만 있거나 get , set 메서드만 있는 인덱싱된 속성을 정의하는 방법을 보여줍니다. get 전용으로 표시된 구문과 집합에만 표시된 구문을 결합하고 가져오기 및 집합이 모두 있는 속성을 생성할 수도 있습니다. 이 후자의 양식을 사용하면 get 및 set 메서드에 다른 접근성 한정자와 특성을 배치할 수 있습니다.

컴파일러는 이름을 Item사용하여 속성을 기본 인덱싱된 속성으로 처리합니다. 기본 인덱싱된 속성은 개체 인스턴스에서 배열과 유사한 구문을 사용하여 액세스할 수 있는 속성입니다. 예를 들어 이 속성을 정의하는 형식의 개체인 경우 o 구문을 o[index] 사용하여 속성에 액세스합니다.

기본값이 아닌 인덱싱된 속성에 액세스하는 구문은 일반 멤버처럼 속성의 이름과 인덱스를 괄호로 제공하는 것입니다. 예를 들어 속성이 o 호출 Ordinal되면 액세스하기 위해 작성 o.Ordinal(index) 합니다.

어떤 폼을 사용하든 인덱싱된 속성에서 set 메서드에 대해 항상 curried 양식을 사용해야 합니다. 커리 함수에 대한 자세한 내용은 Functions를 참조 하세요.

F# 6 이전에는 인덱싱에 구문 expr.[idx] 이 사용되었습니다. 선택적 정보 경고(/warnon:3566 또는 속성 <WarnOn>3566</WarnOn>)를 활성화하여 표기법 사용을 expr.[idx] 보고할 수 있습니다.

예시

다음 코드 예제에서는 get 및 set 메서드가 있는 기본 및 기본이 아닌 인덱싱된 속성의 정의 및 사용을 보여 줍니다.

type NumberStrings() =
    let mutable ordinals =
        [| "one"
           "two"
           "three"
           "four"
           "five"
           "six"
           "seven"
           "eight"
           "nine"
           "ten" |]

    let mutable cardinals =
        [| "first"
           "second"
           "third"
           "fourth"
           "fifth"
           "sixth"
           "seventh"
           "eighth"
           "ninth"
           "tenth" |]

    member this.Item
        with get (index) = ordinals[index]
        and set index value = ordinals[index] <- value

    member this.Ordinal
        with get (index) = ordinals[index]
        and set index value = ordinals[index] <- value

    member this.Cardinal
        with get (index) = cardinals[index]
        and set index value = cardinals[index] <- value

let nstrs = new NumberStrings()
nstrs[0] <- "ONE"

for i in 0..9 do
    printf "%s " nstrs[i]

printfn ""

nstrs.Cardinal(5) <- "6th"

for i in 0..9 do
    printf "%s " (nstrs.Ordinal(i))
    printf "%s " (nstrs.Cardinal(i))

printfn ""

출력

ONE two three four five six seven eight nine ten
ONE first two second three third four fourth five fifth six 6th
seven seventh eight eighth nine ninth ten tenth

여러 인덱스 값이 있는 인덱싱된 속성

인덱싱된 속성에는 둘 이상의 인덱스 값이 있을 수 있습니다. 이 경우 속성이 사용될 때 값은 쉼표로 구분됩니다. 이러한 속성의 set 메서드에는 두 개의 커리 인수가 있어야 합니다. 그 중 첫 번째는 키가 포함된 튜플이고 두 번째 인수는 설정할 값입니다.

다음 코드에서는 여러 인덱스 값으로 인덱싱된 속성을 사용하는 방법을 보여 줍니다.

open System.Collections.Generic

/// Basic implementation of a sparse matrix based on a dictionary
type SparseMatrix() =
    let table = new Dictionary<(int * int), float>()
    member _.Item
        // Because the key is comprised of two values, 'get' has two index values
        with get(key1, key2) = table[(key1, key2)]

        // 'set' has two index values and a new value to place in the key's position
        and set (key1, key2) value = table[(key1, key2)] <- value

let sm = new SparseMatrix()
for i in 1..1000 do
    sm[i, i] <- float i * float i

참고 항목