Scala - value < is not a member of AnyVal












4















Here is my code. I got a compile error at second case clause:




Error:(92, 26) value < is not a member of AnyVal
case x if x._2 < 2 => "price under 2"




def tupleMatch: Unit = {
val donut = Tuple2("Donut", 2.5)
val plain = Tuple2("Plain", 1.0)
val tuples = List(donut, plain)
tuples.foreach { tuple =>
val toPrint = tuple match {
case ("Donut", price) => s"price of Donut is ${donut._2}"
case (_, price) if price < 2 => "price under 2"
case _ => "other"
}
println(toPrint)

}
}


After I change



val plain = Tuple2("Plain", 1)


to



val plain = Tuple2("Plain", 1.0)


it finally works.
It makes me confusing, I want to know why?










share|improve this question





























    4















    Here is my code. I got a compile error at second case clause:




    Error:(92, 26) value < is not a member of AnyVal
    case x if x._2 < 2 => "price under 2"




    def tupleMatch: Unit = {
    val donut = Tuple2("Donut", 2.5)
    val plain = Tuple2("Plain", 1.0)
    val tuples = List(donut, plain)
    tuples.foreach { tuple =>
    val toPrint = tuple match {
    case ("Donut", price) => s"price of Donut is ${donut._2}"
    case (_, price) if price < 2 => "price under 2"
    case _ => "other"
    }
    println(toPrint)

    }
    }


    After I change



    val plain = Tuple2("Plain", 1)


    to



    val plain = Tuple2("Plain", 1.0)


    it finally works.
    It makes me confusing, I want to know why?










    share|improve this question



























      4












      4








      4








      Here is my code. I got a compile error at second case clause:




      Error:(92, 26) value < is not a member of AnyVal
      case x if x._2 < 2 => "price under 2"




      def tupleMatch: Unit = {
      val donut = Tuple2("Donut", 2.5)
      val plain = Tuple2("Plain", 1.0)
      val tuples = List(donut, plain)
      tuples.foreach { tuple =>
      val toPrint = tuple match {
      case ("Donut", price) => s"price of Donut is ${donut._2}"
      case (_, price) if price < 2 => "price under 2"
      case _ => "other"
      }
      println(toPrint)

      }
      }


      After I change



      val plain = Tuple2("Plain", 1)


      to



      val plain = Tuple2("Plain", 1.0)


      it finally works.
      It makes me confusing, I want to know why?










      share|improve this question
















      Here is my code. I got a compile error at second case clause:




      Error:(92, 26) value < is not a member of AnyVal
      case x if x._2 < 2 => "price under 2"




      def tupleMatch: Unit = {
      val donut = Tuple2("Donut", 2.5)
      val plain = Tuple2("Plain", 1.0)
      val tuples = List(donut, plain)
      tuples.foreach { tuple =>
      val toPrint = tuple match {
      case ("Donut", price) => s"price of Donut is ${donut._2}"
      case (_, price) if price < 2 => "price under 2"
      case _ => "other"
      }
      println(toPrint)

      }
      }


      After I change



      val plain = Tuple2("Plain", 1)


      to



      val plain = Tuple2("Plain", 1.0)


      it finally works.
      It makes me confusing, I want to know why?







      scala functional-programming






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 16:36









      Amit Prasad

      573316




      573316










      asked Nov 16 '18 at 9:53









      Felix JFelix J

      314




      314
























          3 Answers
          3






          active

          oldest

          votes


















          5














          That is because your tuples array is formed by two different types: Tuple2[String, Int] and Tuple2[String, Double]. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]. When you put a Double representation the compiler is capable of create the Tuple2[String, Double].






          share|improve this answer

































            5














            Looking at the REPL:



            scala> val donut = Tuple2("Donut", 2.5)
            donut: (String, Double) = (Donut,2.5)

            scala> val plain = Tuple2("Plain", 1.0)
            plain: (String, Double) = (Plain,1.0)

            scala> val tuples = List(donut, plain)
            tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


            Because the two Tuples have the same type (String, Double), the List is of type List[(String, Double)]. < is a valid function for Double.



            When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int vs Double) so the resulting List is List[(String, AnyVal)]:



            scala> val donut = Tuple2("Donut", 2.5)
            donut: (String, Double) = (Donut,2.5)

            scala> val plain = Tuple2("Plain", 1)
            plain: (String, Int) = (Plain,1)

            scala> val tuples = List(donut, plain)
            tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


            ... and < is not a valid function for AnyVal.






            share|improve this answer































              2














              It because of we cannot perform < (less than) operation on AnyVal type.



              scala> val donut = Tuple2("Donut", 2.5)
              donut: (String, Double) = (Donut,2.5)

              scala> val plain = Tuple2("Plain", 1)
              plain: (String, Int) = (Plain,1)


              As you can see above. When you define Tuple2("Donut", 2.5), 2.5 is inferred to Double and in Tuple2("Plain", 1), 1 is inferred as Int since you have not provided type explicitly.



              Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal).



              scala> val tuples = List(donut, plain)
              tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


              Now, when you are performing case x if x._2 < 2 you are actually comparing AnyVal type to number i.e. 2, which is not possible because compiler thinks that x._2 is AnyVal(not number) and won't compile. Therefore, you are getting exception at compile time.



              <console>:22: error: value < is not a member of AnyVal
              case x if (x._2 < 2) => "price under 2"


              In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0), its number type is also inferred as Double.



              scala> val donut = Tuple2("Donut", 2.5)
              donut: (String, Double) = (Donut,2.5)

              scala> val plain = Tuple2("Plain", 1.0)
              plain: (String, Double) = (Plain,1.0)


              And, when you create list of tuples, since both have same type i.e. (String, Double), list will have type List[(String, Double)].



              scala> val tuples = List(donut, plain)
              tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


              And x._2 < 2 will compile correctly since you are invoking < in double (number).






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335311%2fscala-value-is-not-a-member-of-anyval%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                5














                That is because your tuples array is formed by two different types: Tuple2[String, Int] and Tuple2[String, Double]. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]. When you put a Double representation the compiler is capable of create the Tuple2[String, Double].






                share|improve this answer






























                  5














                  That is because your tuples array is formed by two different types: Tuple2[String, Int] and Tuple2[String, Double]. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]. When you put a Double representation the compiler is capable of create the Tuple2[String, Double].






                  share|improve this answer




























                    5












                    5








                    5







                    That is because your tuples array is formed by two different types: Tuple2[String, Int] and Tuple2[String, Double]. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]. When you put a Double representation the compiler is capable of create the Tuple2[String, Double].






                    share|improve this answer















                    That is because your tuples array is formed by two different types: Tuple2[String, Int] and Tuple2[String, Double]. These types are inferred by the compiler and the interfered type of tuples array is then Tuple2[String, AnyVal]. When you put a Double representation the compiler is capable of create the Tuple2[String, Double].







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 16 '18 at 10:18









                    peterschrott

                    376219




                    376219










                    answered Nov 16 '18 at 10:12









                    EmiCareOfCell44EmiCareOfCell44

                    1,277415




                    1,277415

























                        5














                        Looking at the REPL:



                        scala> val donut = Tuple2("Donut", 2.5)
                        donut: (String, Double) = (Donut,2.5)

                        scala> val plain = Tuple2("Plain", 1.0)
                        plain: (String, Double) = (Plain,1.0)

                        scala> val tuples = List(donut, plain)
                        tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                        Because the two Tuples have the same type (String, Double), the List is of type List[(String, Double)]. < is a valid function for Double.



                        When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int vs Double) so the resulting List is List[(String, AnyVal)]:



                        scala> val donut = Tuple2("Donut", 2.5)
                        donut: (String, Double) = (Donut,2.5)

                        scala> val plain = Tuple2("Plain", 1)
                        plain: (String, Int) = (Plain,1)

                        scala> val tuples = List(donut, plain)
                        tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                        ... and < is not a valid function for AnyVal.






                        share|improve this answer




























                          5














                          Looking at the REPL:



                          scala> val donut = Tuple2("Donut", 2.5)
                          donut: (String, Double) = (Donut,2.5)

                          scala> val plain = Tuple2("Plain", 1.0)
                          plain: (String, Double) = (Plain,1.0)

                          scala> val tuples = List(donut, plain)
                          tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                          Because the two Tuples have the same type (String, Double), the List is of type List[(String, Double)]. < is a valid function for Double.



                          When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int vs Double) so the resulting List is List[(String, AnyVal)]:



                          scala> val donut = Tuple2("Donut", 2.5)
                          donut: (String, Double) = (Donut,2.5)

                          scala> val plain = Tuple2("Plain", 1)
                          plain: (String, Int) = (Plain,1)

                          scala> val tuples = List(donut, plain)
                          tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                          ... and < is not a valid function for AnyVal.






                          share|improve this answer


























                            5












                            5








                            5







                            Looking at the REPL:



                            scala> val donut = Tuple2("Donut", 2.5)
                            donut: (String, Double) = (Donut,2.5)

                            scala> val plain = Tuple2("Plain", 1.0)
                            plain: (String, Double) = (Plain,1.0)

                            scala> val tuples = List(donut, plain)
                            tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                            Because the two Tuples have the same type (String, Double), the List is of type List[(String, Double)]. < is a valid function for Double.



                            When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int vs Double) so the resulting List is List[(String, AnyVal)]:



                            scala> val donut = Tuple2("Donut", 2.5)
                            donut: (String, Double) = (Donut,2.5)

                            scala> val plain = Tuple2("Plain", 1)
                            plain: (String, Int) = (Plain,1)

                            scala> val tuples = List(donut, plain)
                            tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                            ... and < is not a valid function for AnyVal.






                            share|improve this answer













                            Looking at the REPL:



                            scala> val donut = Tuple2("Donut", 2.5)
                            donut: (String, Double) = (Donut,2.5)

                            scala> val plain = Tuple2("Plain", 1.0)
                            plain: (String, Double) = (Plain,1.0)

                            scala> val tuples = List(donut, plain)
                            tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                            Because the two Tuples have the same type (String, Double), the List is of type List[(String, Double)]. < is a valid function for Double.



                            When you define plain as Tuple2("Plain", 1), the second part of the two Tuples are of different types (Int vs Double) so the resulting List is List[(String, AnyVal)]:



                            scala> val donut = Tuple2("Donut", 2.5)
                            donut: (String, Double) = (Donut,2.5)

                            scala> val plain = Tuple2("Plain", 1)
                            plain: (String, Int) = (Plain,1)

                            scala> val tuples = List(donut, plain)
                            tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                            ... and < is not a valid function for AnyVal.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 10:20









                            James WhiteleyJames Whiteley

                            2,246829




                            2,246829























                                2














                                It because of we cannot perform < (less than) operation on AnyVal type.



                                scala> val donut = Tuple2("Donut", 2.5)
                                donut: (String, Double) = (Donut,2.5)

                                scala> val plain = Tuple2("Plain", 1)
                                plain: (String, Int) = (Plain,1)


                                As you can see above. When you define Tuple2("Donut", 2.5), 2.5 is inferred to Double and in Tuple2("Plain", 1), 1 is inferred as Int since you have not provided type explicitly.



                                Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal).



                                scala> val tuples = List(donut, plain)
                                tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                                Now, when you are performing case x if x._2 < 2 you are actually comparing AnyVal type to number i.e. 2, which is not possible because compiler thinks that x._2 is AnyVal(not number) and won't compile. Therefore, you are getting exception at compile time.



                                <console>:22: error: value < is not a member of AnyVal
                                case x if (x._2 < 2) => "price under 2"


                                In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0), its number type is also inferred as Double.



                                scala> val donut = Tuple2("Donut", 2.5)
                                donut: (String, Double) = (Donut,2.5)

                                scala> val plain = Tuple2("Plain", 1.0)
                                plain: (String, Double) = (Plain,1.0)


                                And, when you create list of tuples, since both have same type i.e. (String, Double), list will have type List[(String, Double)].



                                scala> val tuples = List(donut, plain)
                                tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                                And x._2 < 2 will compile correctly since you are invoking < in double (number).






                                share|improve this answer




























                                  2














                                  It because of we cannot perform < (less than) operation on AnyVal type.



                                  scala> val donut = Tuple2("Donut", 2.5)
                                  donut: (String, Double) = (Donut,2.5)

                                  scala> val plain = Tuple2("Plain", 1)
                                  plain: (String, Int) = (Plain,1)


                                  As you can see above. When you define Tuple2("Donut", 2.5), 2.5 is inferred to Double and in Tuple2("Plain", 1), 1 is inferred as Int since you have not provided type explicitly.



                                  Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal).



                                  scala> val tuples = List(donut, plain)
                                  tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                                  Now, when you are performing case x if x._2 < 2 you are actually comparing AnyVal type to number i.e. 2, which is not possible because compiler thinks that x._2 is AnyVal(not number) and won't compile. Therefore, you are getting exception at compile time.



                                  <console>:22: error: value < is not a member of AnyVal
                                  case x if (x._2 < 2) => "price under 2"


                                  In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0), its number type is also inferred as Double.



                                  scala> val donut = Tuple2("Donut", 2.5)
                                  donut: (String, Double) = (Donut,2.5)

                                  scala> val plain = Tuple2("Plain", 1.0)
                                  plain: (String, Double) = (Plain,1.0)


                                  And, when you create list of tuples, since both have same type i.e. (String, Double), list will have type List[(String, Double)].



                                  scala> val tuples = List(donut, plain)
                                  tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                                  And x._2 < 2 will compile correctly since you are invoking < in double (number).






                                  share|improve this answer


























                                    2












                                    2








                                    2







                                    It because of we cannot perform < (less than) operation on AnyVal type.



                                    scala> val donut = Tuple2("Donut", 2.5)
                                    donut: (String, Double) = (Donut,2.5)

                                    scala> val plain = Tuple2("Plain", 1)
                                    plain: (String, Int) = (Plain,1)


                                    As you can see above. When you define Tuple2("Donut", 2.5), 2.5 is inferred to Double and in Tuple2("Plain", 1), 1 is inferred as Int since you have not provided type explicitly.



                                    Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal).



                                    scala> val tuples = List(donut, plain)
                                    tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                                    Now, when you are performing case x if x._2 < 2 you are actually comparing AnyVal type to number i.e. 2, which is not possible because compiler thinks that x._2 is AnyVal(not number) and won't compile. Therefore, you are getting exception at compile time.



                                    <console>:22: error: value < is not a member of AnyVal
                                    case x if (x._2 < 2) => "price under 2"


                                    In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0), its number type is also inferred as Double.



                                    scala> val donut = Tuple2("Donut", 2.5)
                                    donut: (String, Double) = (Donut,2.5)

                                    scala> val plain = Tuple2("Plain", 1.0)
                                    plain: (String, Double) = (Plain,1.0)


                                    And, when you create list of tuples, since both have same type i.e. (String, Double), list will have type List[(String, Double)].



                                    scala> val tuples = List(donut, plain)
                                    tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                                    And x._2 < 2 will compile correctly since you are invoking < in double (number).






                                    share|improve this answer













                                    It because of we cannot perform < (less than) operation on AnyVal type.



                                    scala> val donut = Tuple2("Donut", 2.5)
                                    donut: (String, Double) = (Donut,2.5)

                                    scala> val plain = Tuple2("Plain", 1)
                                    plain: (String, Int) = (Plain,1)


                                    As you can see above. When you define Tuple2("Donut", 2.5), 2.5 is inferred to Double and in Tuple2("Plain", 1), 1 is inferred as Int since you have not provided type explicitly.



                                    Now, when you create list of these tuples, because of type inference list type will be (String, AnyVal).



                                    scala> val tuples = List(donut, plain)
                                    tuples: List[(String, AnyVal)] = List((Donut,2.5), (Plain,1))


                                    Now, when you are performing case x if x._2 < 2 you are actually comparing AnyVal type to number i.e. 2, which is not possible because compiler thinks that x._2 is AnyVal(not number) and won't compile. Therefore, you are getting exception at compile time.



                                    <console>:22: error: value < is not a member of AnyVal
                                    case x if (x._2 < 2) => "price under 2"


                                    In your second case, i.e. when you defined tuple as Tuple2("Plain", 1.0), its number type is also inferred as Double.



                                    scala> val donut = Tuple2("Donut", 2.5)
                                    donut: (String, Double) = (Donut,2.5)

                                    scala> val plain = Tuple2("Plain", 1.0)
                                    plain: (String, Double) = (Plain,1.0)


                                    And, when you create list of tuples, since both have same type i.e. (String, Double), list will have type List[(String, Double)].



                                    scala> val tuples = List(donut, plain)
                                    tuples: List[(String, Double)] = List((Donut,2.5), (Plain,1.0))


                                    And x._2 < 2 will compile correctly since you are invoking < in double (number).







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 16 '18 at 10:20









                                    Ra KaRa Ka

                                    1,73011326




                                    1,73011326






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335311%2fscala-value-is-not-a-member-of-anyval%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Bressuire

                                        Vorschmack

                                        Quarantine