Is “*” the same as “*” in scala?










0














I see a piece of code as



def poly(x: Int): Int = x*x - 2*x +1


the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *? Is * the same as *?










share|improve this question



















  • 3




    I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
    – prayagupd
    Nov 11 at 5:36







  • 1




    it's not the valid syntax can you please tell me where have you seen such code?
    – Raman Mishra
    Nov 11 at 5:37










  • @prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
    – Jörg W Mittag
    Nov 11 at 9:36










  • @RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
    – Jörg W Mittag
    Nov 11 at 9:37











  • @JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
    – Raman Mishra
    Nov 11 at 10:11















0














I see a piece of code as



def poly(x: Int): Int = x*x - 2*x +1


the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *? Is * the same as *?










share|improve this question



















  • 3




    I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
    – prayagupd
    Nov 11 at 5:36







  • 1




    it's not the valid syntax can you please tell me where have you seen such code?
    – Raman Mishra
    Nov 11 at 5:37










  • @prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
    – Jörg W Mittag
    Nov 11 at 9:36










  • @RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
    – Jörg W Mittag
    Nov 11 at 9:37











  • @JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
    – Raman Mishra
    Nov 11 at 10:11













0












0








0







I see a piece of code as



def poly(x: Int): Int = x*x - 2*x +1


the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *? Is * the same as *?










share|improve this question















I see a piece of code as



def poly(x: Int): Int = x*x - 2*x +1


the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *? Is * the same as *?







scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:42









jwvh

25.3k52038




25.3k52038










asked Nov 11 at 5:31









Yong Chen

111




111







  • 3




    I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
    – prayagupd
    Nov 11 at 5:36







  • 1




    it's not the valid syntax can you please tell me where have you seen such code?
    – Raman Mishra
    Nov 11 at 5:37










  • @prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
    – Jörg W Mittag
    Nov 11 at 9:36










  • @RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
    – Jörg W Mittag
    Nov 11 at 9:37











  • @JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
    – Raman Mishra
    Nov 11 at 10:11












  • 3




    I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
    – prayagupd
    Nov 11 at 5:36







  • 1




    it's not the valid syntax can you please tell me where have you seen such code?
    – Raman Mishra
    Nov 11 at 5:37










  • @prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
    – Jörg W Mittag
    Nov 11 at 9:36










  • @RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
    – Jörg W Mittag
    Nov 11 at 9:37











  • @JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
    – Raman Mishra
    Nov 11 at 10:11







3




3




I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
– prayagupd
Nov 11 at 5:36





I don't think * is even valid syntax? unless it is defined as a custom method like def *(x: Int) = x * x can you please post where did you see that?
– prayagupd
Nov 11 at 5:36





1




1




it's not the valid syntax can you please tell me where have you seen such code?
– Raman Mishra
Nov 11 at 5:37




it's not the valid syntax can you please tell me where have you seen such code?
– Raman Mishra
Nov 11 at 5:37












@prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
– Jörg W Mittag
Nov 11 at 9:36




@prayagupd: "I don't think * is even valid syntax?" – Yes, it is valid syntax. It is a valid identifier, just like foo or bar or ??? or *. "unless it is defined as a custom method like def *(x: Int) = x * x" – This makes no sense. If it isn't valid syntax (like you claim), then you couldn't define a method with that name. If you could define a method with that name, then it obviously must be valid syntax. It cannot be both invalid syntax and a valid method name, that statement is contradictory.
– Jörg W Mittag
Nov 11 at 9:36












@RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
– Jörg W Mittag
Nov 11 at 9:37





@RamanMishra: It is valid syntax. It is a valid method name just like foo or bar or ??? or *.
– Jörg W Mittag
Nov 11 at 9:37













@JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
– Raman Mishra
Nov 11 at 10:11




@JörgWMittag yes i agree its a valid method name if defined and in the question i don't see any user defined method name /*.
– Raman Mishra
Nov 11 at 10:11












2 Answers
2






active

oldest

votes


















1














It doesn't exist in vanilla Scala. It would be possible to define * as a method in an implicit class:



implicit class IntOps(x: Int) 
def *(y: Int) = x * y



Which can be used in the same way as you're seeing: 6 * 7



But to use this new method * in your code, the implicit class must be in scope or imported.



However, I suspect that none of the above is applicable here. It could be the case that there's just an error in displaying the code which instead should read:



def poly(x: Int): Int = x*x - 2*x +1






share|improve this answer




























    0















    I see a piece of code as



    def poly(x: Int): Int = x*x - 2*x +1


    the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *?




    That is simply the name of the method. * is just as valid as a method name as * or foo or bar or ???. It's just a method like any other method.




    Is * the same as *?




    No, * is * and * is *. They are different methods, just like foo and bar are different methods.






    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%2f53246110%2fis-the-same-as-in-scala%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














      It doesn't exist in vanilla Scala. It would be possible to define * as a method in an implicit class:



      implicit class IntOps(x: Int) 
      def *(y: Int) = x * y



      Which can be used in the same way as you're seeing: 6 * 7



      But to use this new method * in your code, the implicit class must be in scope or imported.



      However, I suspect that none of the above is applicable here. It could be the case that there's just an error in displaying the code which instead should read:



      def poly(x: Int): Int = x*x - 2*x +1






      share|improve this answer

























        1














        It doesn't exist in vanilla Scala. It would be possible to define * as a method in an implicit class:



        implicit class IntOps(x: Int) 
        def *(y: Int) = x * y



        Which can be used in the same way as you're seeing: 6 * 7



        But to use this new method * in your code, the implicit class must be in scope or imported.



        However, I suspect that none of the above is applicable here. It could be the case that there's just an error in displaying the code which instead should read:



        def poly(x: Int): Int = x*x - 2*x +1






        share|improve this answer























          1












          1








          1






          It doesn't exist in vanilla Scala. It would be possible to define * as a method in an implicit class:



          implicit class IntOps(x: Int) 
          def *(y: Int) = x * y



          Which can be used in the same way as you're seeing: 6 * 7



          But to use this new method * in your code, the implicit class must be in scope or imported.



          However, I suspect that none of the above is applicable here. It could be the case that there's just an error in displaying the code which instead should read:



          def poly(x: Int): Int = x*x - 2*x +1






          share|improve this answer












          It doesn't exist in vanilla Scala. It would be possible to define * as a method in an implicit class:



          implicit class IntOps(x: Int) 
          def *(y: Int) = x * y



          Which can be used in the same way as you're seeing: 6 * 7



          But to use this new method * in your code, the implicit class must be in scope or imported.



          However, I suspect that none of the above is applicable here. It could be the case that there's just an error in displaying the code which instead should read:



          def poly(x: Int): Int = x*x - 2*x +1







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 23:18









          Tom

          1,6892924




          1,6892924























              0















              I see a piece of code as



              def poly(x: Int): Int = x*x - 2*x +1


              the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *?




              That is simply the name of the method. * is just as valid as a method name as * or foo or bar or ???. It's just a method like any other method.




              Is * the same as *?




              No, * is * and * is *. They are different methods, just like foo and bar are different methods.






              share|improve this answer

























                0















                I see a piece of code as



                def poly(x: Int): Int = x*x - 2*x +1


                the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *?




                That is simply the name of the method. * is just as valid as a method name as * or foo or bar or ???. It's just a method like any other method.




                Is * the same as *?




                No, * is * and * is *. They are different methods, just like foo and bar are different methods.






                share|improve this answer























                  0












                  0








                  0







                  I see a piece of code as



                  def poly(x: Int): Int = x*x - 2*x +1


                  the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *?




                  That is simply the name of the method. * is just as valid as a method name as * or foo or bar or ???. It's just a method like any other method.




                  Is * the same as *?




                  No, * is * and * is *. They are different methods, just like foo and bar are different methods.






                  share|improve this answer













                  I see a piece of code as



                  def poly(x: Int): Int = x*x - 2*x +1


                  the symbol * confused me. It seems that this * works like multiply, i.e. *, but why there is a ahead of *?




                  That is simply the name of the method. * is just as valid as a method name as * or foo or bar or ???. It's just a method like any other method.




                  Is * the same as *?




                  No, * is * and * is *. They are different methods, just like foo and bar are different methods.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 9:39









                  Jörg W Mittag

                  288k62353546




                  288k62353546



























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53246110%2fis-the-same-as-in-scala%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

                      Darth Vader #20

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Ondo