복붙노트

[SCALA] 왜 매개 변수 목록이없는 경우 클래스는 사용되지 않는?

SCALA

왜 매개 변수 목록이없는 경우 클래스는 사용되지 않는?

왜 매개 변수 목록이없는 경우 클래스는 스칼라에서 사용되지 않는? 왜 컴파일러 대신 매개 변수 목록로 사용 ()에 제안 하는가?

편집하다 :

누군가가 ... 내 두 번째 질문에 대답하십시오 |

해결법

  1. ==============================

    1.그것은 실수로 패턴으로 잘못 인수 없음의 경우 클래스를 사용하는 정말 쉽습니다.

    그것은 실수로 패턴으로 잘못 인수 없음의 경우 클래스를 사용하는 정말 쉽습니다.

    scala> case class Foo                                             
    warning: there were deprecation warnings; re-run with -deprecation for details
    defined class Foo
    
    scala> (new Foo: Any) match { case Foo => true; case _ => false } 
    res10: Boolean = false
    

    대신에:

    scala> (new Foo: Any) match { case _: Foo => true; case _ => false } 
    res11: Boolean = true
    

    또는 더 나은 :

    scala> case object Bar                                               
    defined module Bar
    
    scala> (Bar: Any) match { case Bar => true; case _ => false }        
    res12: Boolean = true
    

    빈 매개 변수 목록이 사용되지 않는 매개 변수를 누락 한 목록에 선호하는 이유 UPDATE 희망 아래의 성적 증명서 시연 할 예정이다.

    scala> case class Foo() // Using an empty parameter list rather than zero parameter lists.
    defined class Foo
    
    scala> Foo // Access the companion object Foo
    res0: Foo.type = <function0>
    
    scala> Foo() // Call Foo.apply() to construct an instance of class Foo
    res1: Foo = Foo()
    
    scala> case class Bar
    warning: there were deprecation warnings; re-run with -deprecation for details
    defined class Bar
    
    scala> Bar // You may expect this to construct a new instance of class Bar, but instead
               // it references the companion object Bar 
    res2: Bar.type = <function0>
    
    scala> Bar() // This calls Bar.apply(), but is not symmetrical with the class definition.
    res3: Bar = Bar()
    
    scala> Bar.apply // Another way to call Bar.apply
    res4: Bar = Bar()
    

    사례 개체는 일반적으로 여전히 빈 매개 변수 목록보다 선호 될 것이다.

  2. ==============================

    2.파라미터없이, 케이스 클래스의 각 인스턴스는 구별하고, 따라서 본질적으로 일정하다. 이 경우에 대한 객체를 사용합니다.

    파라미터없이, 케이스 클래스의 각 인스턴스는 구별하고, 따라서 본질적으로 일정하다. 이 경우에 대한 객체를 사용합니다.

  3. from https://stackoverflow.com/questions/2254710/why-were-the-case-classes-without-a-parameter-list-deprecated by cc-by-sa and MIT license