IT

스칼라에서`#`연산자는 무엇을 의미합니까?

lottoking 2020. 7. 13. 07:48
반응형

스칼라에서`#`연산자는 무엇을 의미합니까?


이 블로그 에서이 코드를 볼 수 있습니다 : Scala의 유형 수준 프로그래밍 :

// define the abstract types and bounds
trait Recurse {
  type Next <: Recurse
  // this is the recursive function definition
  type X[R <: Recurse] <: Int
}
// implementation
trait RecurseA extends Recurse {
  type Next = RecurseA
  // this is the implementation
  type X[R <: Recurse] = R#X[R#Next]
}
object Recurse {
  // infinite loop
  type C = RecurseA#X[RecurseA]
}

내가 본 적이없는 #코드에 연산자 R#X[R#Next]있습니다. 검색 엔진이 무시하기 때문에 검색하기 어렵 기 때문에 누가 무슨 뜻인지 알 수 있습니까?


이를 설명하기 위해 먼저 스칼라에서 중첩 클래스를 설명해야합니다. 이 간단한 예를 고려하십시오.

class A {
  class B

  def f(b: B) = println("Got my B!")
}

이제 예제를 시도해 보자.

scala> val a1 = new A
a1: A = A@2fa8ecf4

scala> val a2 = new A
a2: A = A@4bed4c8

scala> a2.f(new a1.B)
<console>:11: error: type mismatch;
 found   : a1.B
 required: a2.B
              a2.f(new a1.B)
                   ^

스텁의 다른 클래스를 내부에서 클래스를 선언하면 해당 클래스의 각 인스턴스 에 하위 클래스가 칼라 말하는 것입니다 . 즉, A.B클래스 가 없지만 클래스가 a1.B있으며 a2.B클래스가 다르며 오류 메시지가 위에서 알려 주듯이 서로 다른 클래스입니다.

이해하지 못한 경로 경로를 찾으십시오.

이제 #특정 인스턴스로 제한하지 않고 중첩 클래스를 참조 할 수 있습니다. 즉, 더 없다 A.B, 거기 A#B의미 B의 중첩 클래스 임의 의 인스턴스를 A.

위의 코드를 변경하여 작업에서 확인할 수 있습니다.

class A {
  class B

  def f(b: B) = println("Got my B!")
  def g(b: A#B) = println("Got a B.")
}

그리고 시도 시도 :

scala> val a1 = new A
a1: A = A@1497b7b1

scala> val a2 = new A
a2: A = A@2607c28c

scala> a2.f(new a1.B)
<console>:11: error: type mismatch;
 found   : a1.B
 required: a2.B
              a2.f(new a1.B)
                   ^

scala> a2.g(new a1.B)
Got a B.

유형 프로젝션이라고하며 유형 멤버에 액세스하는 데 사용됩니다.

scala> trait R {
     |   type A = Int
     | }
defined trait R

scala> val x = null.asInstanceOf[R#A]
x: Int = 0

기본적으로 다른 클래스 내의 클래스를 참조하는 방법입니다.

http://jim-mcbeath.blogspot.com/2008/09/scala-syntax-primer.html ( "파운드"검색)


여기에 "기호 연산자"(실제로는 메서드) 검색을위한 리소스가 scalex에서 검색하기 위해 "#"을 이스케이프하는 방법을 찾지 못합니다.)

http://www.artima.com/pins1ed/book-index.html#indexanchor

참고 URL : https://stackoverflow.com/questions/9443004/what-does-the-operator-mean-in-scala

반응형