what is difference between (1 to 10 by 3) toList and (1 to 10 by 3).toList in scala(or intellij problem?)












0















if this question is stupid, please understand.



I'm starting to learn scala with basic grammar.



But I don't understand why one is error other is not. I tested this with scala worksheet



val range = 1 to 10 by 3 toList
println(s"$range") //error


but the below is the List(actually Seq)



val range = (1 to 10 by 3).toList
println(s"$range") //no problem


I am using intellij 2018.2 ultimate edit.



Edit: Image was changed. I attached capture that is included error message not warning.
enter image description here



Edit: I thought it is intellij version problem, but still doesn't work










share|improve this question




















  • 1





    That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

    – Sarvesh Kumar Singh
    Nov 22 '18 at 8:50













  • it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

    – Raf
    Nov 22 '18 at 8:56











  • you can go into scala-> general -> advance language feature (tick mark it)

    – Raman Mishra
    Nov 22 '18 at 8:57











  • It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

    – jwvh
    Nov 22 '18 at 8:58






  • 1





    @HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

    – prayagupd
    Nov 22 '18 at 9:43


















0















if this question is stupid, please understand.



I'm starting to learn scala with basic grammar.



But I don't understand why one is error other is not. I tested this with scala worksheet



val range = 1 to 10 by 3 toList
println(s"$range") //error


but the below is the List(actually Seq)



val range = (1 to 10 by 3).toList
println(s"$range") //no problem


I am using intellij 2018.2 ultimate edit.



Edit: Image was changed. I attached capture that is included error message not warning.
enter image description here



Edit: I thought it is intellij version problem, but still doesn't work










share|improve this question




















  • 1





    That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

    – Sarvesh Kumar Singh
    Nov 22 '18 at 8:50













  • it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

    – Raf
    Nov 22 '18 at 8:56











  • you can go into scala-> general -> advance language feature (tick mark it)

    – Raman Mishra
    Nov 22 '18 at 8:57











  • It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

    – jwvh
    Nov 22 '18 at 8:58






  • 1





    @HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

    – prayagupd
    Nov 22 '18 at 9:43
















0












0








0








if this question is stupid, please understand.



I'm starting to learn scala with basic grammar.



But I don't understand why one is error other is not. I tested this with scala worksheet



val range = 1 to 10 by 3 toList
println(s"$range") //error


but the below is the List(actually Seq)



val range = (1 to 10 by 3).toList
println(s"$range") //no problem


I am using intellij 2018.2 ultimate edit.



Edit: Image was changed. I attached capture that is included error message not warning.
enter image description here



Edit: I thought it is intellij version problem, but still doesn't work










share|improve this question
















if this question is stupid, please understand.



I'm starting to learn scala with basic grammar.



But I don't understand why one is error other is not. I tested this with scala worksheet



val range = 1 to 10 by 3 toList
println(s"$range") //error


but the below is the List(actually Seq)



val range = (1 to 10 by 3).toList
println(s"$range") //no problem


I am using intellij 2018.2 ultimate edit.



Edit: Image was changed. I attached capture that is included error message not warning.
enter image description here



Edit: I thought it is intellij version problem, but still doesn't work







scala intellij-idea






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 12:30







Hacking J

















asked Nov 22 '18 at 8:40









Hacking JHacking J

11910




11910








  • 1





    That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

    – Sarvesh Kumar Singh
    Nov 22 '18 at 8:50













  • it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

    – Raf
    Nov 22 '18 at 8:56











  • you can go into scala-> general -> advance language feature (tick mark it)

    – Raman Mishra
    Nov 22 '18 at 8:57











  • It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

    – jwvh
    Nov 22 '18 at 8:58






  • 1





    @HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

    – prayagupd
    Nov 22 '18 at 9:43
















  • 1





    That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

    – Sarvesh Kumar Singh
    Nov 22 '18 at 8:50













  • it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

    – Raf
    Nov 22 '18 at 8:56











  • you can go into scala-> general -> advance language feature (tick mark it)

    – Raman Mishra
    Nov 22 '18 at 8:57











  • It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

    – jwvh
    Nov 22 '18 at 8:58






  • 1





    @HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

    – prayagupd
    Nov 22 '18 at 9:43










1




1





That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

– Sarvesh Kumar Singh
Nov 22 '18 at 8:50







That is actually not an error. Its just that IntelliJ is confused by the compiler warning about the usage of postfix syntax. I will strongly advise you to start with more explicit syntax like val range = Range(1, 11, 3).toList or Range.inclusive(1, 10, 3) instead of (1 to 10 by 3).toList which involves the implicits over Int and postfix syntax. Once you have gotten a good grasp of things then you can move to more fancier stuff like this.

– Sarvesh Kumar Singh
Nov 22 '18 at 8:50















it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

– Raf
Nov 22 '18 at 8:56





it'a warning telling you should enable postfixOps if you use them (as you do) import scala.language.postfixOps

– Raf
Nov 22 '18 at 8:56













you can go into scala-> general -> advance language feature (tick mark it)

– Raman Mishra
Nov 22 '18 at 8:57





you can go into scala-> general -> advance language feature (tick mark it)

– Raman Mishra
Nov 22 '18 at 8:57













It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

– jwvh
Nov 22 '18 at 8:58





It's worth noting that the List object has a .range() method: List.range(1,10,3) No .toList conversion needed.

– jwvh
Nov 22 '18 at 8:58




1




1





@HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

– prayagupd
Nov 22 '18 at 9:43







@HackingJ you need to end the line with post-fix operator either with ; or new line or defining a val, def after the post-fix operator.

– prayagupd
Nov 22 '18 at 9:43














2 Answers
2






active

oldest

votes


















1














If you're getting this error ...




Error:(2, 13) recursive lazy value range needs type




... it's because you're using infix (dot-less) notation where you shouldn't.



Put a blank line before the println(), or a semicolon after the toList and it will work.



The reason for the error is that instance.method(argument) can be turned into ...



instance method argument


... but the compiler is often confused if you try to turn instance.method into ...



instance method


The compiler will look for the missing argument until it hits a semicolon or a blank line.






share|improve this answer































    1














    Either should work. In scala you can omit . with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION



    Example:



    scala> val data = "guitar"
    data: String = guitar

    scala> data.toUpperCase
    res8: String = GUITAR


    you can use space instead of .,



    scala> data toUpperCase
    <console>:13: warning: postfix operator toUpperCase should be enabled
    by making the implicit value scala.language.postfixOps visible.
    This can be achieved by adding the import clause 'import scala.language.postfixOps'
    or by setting the compiler option -language:postfixOps.
    See the Scaladoc for value scala.language.postfixOps for a discussion
    why the feature should be explicitly enabled.
    data toUpperCase
    ^
    res9: String = GUITAR


    Since .toUpperCase is used after the data-structure without . (it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,



    scala> import scala.language.postfixOps
    import scala.language.postfixOps

    scala> data toUpperCase
    res10: String = GUITAR


    Same thing applies to your example. From readability perspective dot makes more readable in your example.



    scala> (1 to 10 by 3).toList
    res9: List[Int] = List(1, 4, 7, 10)


    ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt apply the toInt at the end of the computation but maybe what I wanted was 1 + "11".toInt



    ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList with ; or a new line after post-fix operator. Or you can use val, def after post-fix operator that way compiler knows it is different context now.



    scala> :paste

    import scala.language.postfixOps
    val range = 1 to 10 by 3 toList

    println(s"$range")

    // Exiting paste mode, now interpreting.

    List(1, 4, 7, 10)
    import scala.language.postfixOps
    range: List[Int] = List(1, 4, 7, 10)


    Also read: Let’s drop postfix operators






    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%2f53426876%2fwhat-is-difference-between-1-to-10-by-3-tolist-and-1-to-10-by-3-tolist-in-sc%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If you're getting this error ...




      Error:(2, 13) recursive lazy value range needs type




      ... it's because you're using infix (dot-less) notation where you shouldn't.



      Put a blank line before the println(), or a semicolon after the toList and it will work.



      The reason for the error is that instance.method(argument) can be turned into ...



      instance method argument


      ... but the compiler is often confused if you try to turn instance.method into ...



      instance method


      The compiler will look for the missing argument until it hits a semicolon or a blank line.






      share|improve this answer




























        1














        If you're getting this error ...




        Error:(2, 13) recursive lazy value range needs type




        ... it's because you're using infix (dot-less) notation where you shouldn't.



        Put a blank line before the println(), or a semicolon after the toList and it will work.



        The reason for the error is that instance.method(argument) can be turned into ...



        instance method argument


        ... but the compiler is often confused if you try to turn instance.method into ...



        instance method


        The compiler will look for the missing argument until it hits a semicolon or a blank line.






        share|improve this answer


























          1












          1








          1







          If you're getting this error ...




          Error:(2, 13) recursive lazy value range needs type




          ... it's because you're using infix (dot-less) notation where you shouldn't.



          Put a blank line before the println(), or a semicolon after the toList and it will work.



          The reason for the error is that instance.method(argument) can be turned into ...



          instance method argument


          ... but the compiler is often confused if you try to turn instance.method into ...



          instance method


          The compiler will look for the missing argument until it hits a semicolon or a blank line.






          share|improve this answer













          If you're getting this error ...




          Error:(2, 13) recursive lazy value range needs type




          ... it's because you're using infix (dot-less) notation where you shouldn't.



          Put a blank line before the println(), or a semicolon after the toList and it will work.



          The reason for the error is that instance.method(argument) can be turned into ...



          instance method argument


          ... but the compiler is often confused if you try to turn instance.method into ...



          instance method


          The compiler will look for the missing argument until it hits a semicolon or a blank line.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 9:52









          jwvhjwvh

          26.2k52140




          26.2k52140

























              1














              Either should work. In scala you can omit . with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION



              Example:



              scala> val data = "guitar"
              data: String = guitar

              scala> data.toUpperCase
              res8: String = GUITAR


              you can use space instead of .,



              scala> data toUpperCase
              <console>:13: warning: postfix operator toUpperCase should be enabled
              by making the implicit value scala.language.postfixOps visible.
              This can be achieved by adding the import clause 'import scala.language.postfixOps'
              or by setting the compiler option -language:postfixOps.
              See the Scaladoc for value scala.language.postfixOps for a discussion
              why the feature should be explicitly enabled.
              data toUpperCase
              ^
              res9: String = GUITAR


              Since .toUpperCase is used after the data-structure without . (it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,



              scala> import scala.language.postfixOps
              import scala.language.postfixOps

              scala> data toUpperCase
              res10: String = GUITAR


              Same thing applies to your example. From readability perspective dot makes more readable in your example.



              scala> (1 to 10 by 3).toList
              res9: List[Int] = List(1, 4, 7, 10)


              ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt apply the toInt at the end of the computation but maybe what I wanted was 1 + "11".toInt



              ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList with ; or a new line after post-fix operator. Or you can use val, def after post-fix operator that way compiler knows it is different context now.



              scala> :paste

              import scala.language.postfixOps
              val range = 1 to 10 by 3 toList

              println(s"$range")

              // Exiting paste mode, now interpreting.

              List(1, 4, 7, 10)
              import scala.language.postfixOps
              range: List[Int] = List(1, 4, 7, 10)


              Also read: Let’s drop postfix operators






              share|improve this answer






























                1














                Either should work. In scala you can omit . with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION



                Example:



                scala> val data = "guitar"
                data: String = guitar

                scala> data.toUpperCase
                res8: String = GUITAR


                you can use space instead of .,



                scala> data toUpperCase
                <console>:13: warning: postfix operator toUpperCase should be enabled
                by making the implicit value scala.language.postfixOps visible.
                This can be achieved by adding the import clause 'import scala.language.postfixOps'
                or by setting the compiler option -language:postfixOps.
                See the Scaladoc for value scala.language.postfixOps for a discussion
                why the feature should be explicitly enabled.
                data toUpperCase
                ^
                res9: String = GUITAR


                Since .toUpperCase is used after the data-structure without . (it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,



                scala> import scala.language.postfixOps
                import scala.language.postfixOps

                scala> data toUpperCase
                res10: String = GUITAR


                Same thing applies to your example. From readability perspective dot makes more readable in your example.



                scala> (1 to 10 by 3).toList
                res9: List[Int] = List(1, 4, 7, 10)


                ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt apply the toInt at the end of the computation but maybe what I wanted was 1 + "11".toInt



                ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList with ; or a new line after post-fix operator. Or you can use val, def after post-fix operator that way compiler knows it is different context now.



                scala> :paste

                import scala.language.postfixOps
                val range = 1 to 10 by 3 toList

                println(s"$range")

                // Exiting paste mode, now interpreting.

                List(1, 4, 7, 10)
                import scala.language.postfixOps
                range: List[Int] = List(1, 4, 7, 10)


                Also read: Let’s drop postfix operators






                share|improve this answer




























                  1












                  1








                  1







                  Either should work. In scala you can omit . with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION



                  Example:



                  scala> val data = "guitar"
                  data: String = guitar

                  scala> data.toUpperCase
                  res8: String = GUITAR


                  you can use space instead of .,



                  scala> data toUpperCase
                  <console>:13: warning: postfix operator toUpperCase should be enabled
                  by making the implicit value scala.language.postfixOps visible.
                  This can be achieved by adding the import clause 'import scala.language.postfixOps'
                  or by setting the compiler option -language:postfixOps.
                  See the Scaladoc for value scala.language.postfixOps for a discussion
                  why the feature should be explicitly enabled.
                  data toUpperCase
                  ^
                  res9: String = GUITAR


                  Since .toUpperCase is used after the data-structure without . (it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,



                  scala> import scala.language.postfixOps
                  import scala.language.postfixOps

                  scala> data toUpperCase
                  res10: String = GUITAR


                  Same thing applies to your example. From readability perspective dot makes more readable in your example.



                  scala> (1 to 10 by 3).toList
                  res9: List[Int] = List(1, 4, 7, 10)


                  ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt apply the toInt at the end of the computation but maybe what I wanted was 1 + "11".toInt



                  ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList with ; or a new line after post-fix operator. Or you can use val, def after post-fix operator that way compiler knows it is different context now.



                  scala> :paste

                  import scala.language.postfixOps
                  val range = 1 to 10 by 3 toList

                  println(s"$range")

                  // Exiting paste mode, now interpreting.

                  List(1, 4, 7, 10)
                  import scala.language.postfixOps
                  range: List[Int] = List(1, 4, 7, 10)


                  Also read: Let’s drop postfix operators






                  share|improve this answer















                  Either should work. In scala you can omit . with space for method invocation, which is probably broken version of haskell way. I think space is not recommended in scala as per official doc - STYLE GUIDE - METHOD INVOCATION



                  Example:



                  scala> val data = "guitar"
                  data: String = guitar

                  scala> data.toUpperCase
                  res8: String = GUITAR


                  you can use space instead of .,



                  scala> data toUpperCase
                  <console>:13: warning: postfix operator toUpperCase should be enabled
                  by making the implicit value scala.language.postfixOps visible.
                  This can be achieved by adding the import clause 'import scala.language.postfixOps'
                  or by setting the compiler option -language:postfixOps.
                  See the Scaladoc for value scala.language.postfixOps for a discussion
                  why the feature should be explicitly enabled.
                  data toUpperCase
                  ^
                  res9: String = GUITAR


                  Since .toUpperCase is used after the data-structure without . (it is called postfix Operator) and compiler is simply warning about that. It can be fixed as the warning says,



                  scala> import scala.language.postfixOps
                  import scala.language.postfixOps

                  scala> data toUpperCase
                  res10: String = GUITAR


                  Same thing applies to your example. From readability perspective dot makes more readable in your example.



                  scala> (1 to 10 by 3).toList
                  res9: List[Int] = List(1, 4, 7, 10)


                  ALSO, post-fix operators could lead into bugs. For example, 1 + "11" toInt apply the toInt at the end of the computation but maybe what I wanted was 1 + "11".toInt



                  ALSO, ALSO regarding your bug you need to yell at compiler to stop chaining on toList with ; or a new line after post-fix operator. Or you can use val, def after post-fix operator that way compiler knows it is different context now.



                  scala> :paste

                  import scala.language.postfixOps
                  val range = 1 to 10 by 3 toList

                  println(s"$range")

                  // Exiting paste mode, now interpreting.

                  List(1, 4, 7, 10)
                  import scala.language.postfixOps
                  range: List[Int] = List(1, 4, 7, 10)


                  Also read: Let’s drop postfix operators







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 10:07

























                  answered Nov 22 '18 at 8:53









                  prayagupdprayagupd

                  19.7k890138




                  19.7k890138






























                      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%2f53426876%2fwhat-is-difference-between-1-to-10-by-3-tolist-and-1-to-10-by-3-tolist-in-sc%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

                      Wiesbaden

                      Marschland

                      Dieringhausen