Understanding Scala default argument message












0















I am playing around with some Scala code and have met with an error message I don't quite follow. Below is my code



val ignoredIds = Array("one", "two", "three")


def csNotify(x : Any): String = {

case org: String if !ignoredIds.contains(x) =>
println( s" $x should not be here")
"one"
case org : String if ignoredIds.contains(x) =>
println(s"$x should be here")
"two"
}

csNotify("four")


The console output is that I am the arguments for a default function must be known. The error point appears to be pointing at the " String = ". Why would this be the case ? The function should check the two cases and return a string ?










share|improve this question



























    0















    I am playing around with some Scala code and have met with an error message I don't quite follow. Below is my code



    val ignoredIds = Array("one", "two", "three")


    def csNotify(x : Any): String = {

    case org: String if !ignoredIds.contains(x) =>
    println( s" $x should not be here")
    "one"
    case org : String if ignoredIds.contains(x) =>
    println(s"$x should be here")
    "two"
    }

    csNotify("four")


    The console output is that I am the arguments for a default function must be known. The error point appears to be pointing at the " String = ". Why would this be the case ? The function should check the two cases and return a string ?










    share|improve this question

























      0












      0








      0








      I am playing around with some Scala code and have met with an error message I don't quite follow. Below is my code



      val ignoredIds = Array("one", "two", "three")


      def csNotify(x : Any): String = {

      case org: String if !ignoredIds.contains(x) =>
      println( s" $x should not be here")
      "one"
      case org : String if ignoredIds.contains(x) =>
      println(s"$x should be here")
      "two"
      }

      csNotify("four")


      The console output is that I am the arguments for a default function must be known. The error point appears to be pointing at the " String = ". Why would this be the case ? The function should check the two cases and return a string ?










      share|improve this question














      I am playing around with some Scala code and have met with an error message I don't quite follow. Below is my code



      val ignoredIds = Array("one", "two", "three")


      def csNotify(x : Any): String = {

      case org: String if !ignoredIds.contains(x) =>
      println( s" $x should not be here")
      "one"
      case org : String if ignoredIds.contains(x) =>
      println(s"$x should be here")
      "two"
      }

      csNotify("four")


      The console output is that I am the arguments for a default function must be known. The error point appears to be pointing at the " String = ". Why would this be the case ? The function should check the two cases and return a string ?







      scala methods






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 11:19









      SteveSteve

      1,65021217




      1,65021217
























          3 Answers
          3






          active

          oldest

          votes


















          4














          Your case is not finding the match against which it can check your block , and you have missed the match block:



          val ignoredIds = Array("one", "two", "three")


          def csNotify(x : Any): String = x match {

          case org: String if !ignoredIds.contains(x) =>
          println( s" $x should not be here")
          "one"
          case org : String if ignoredIds.contains(x) =>
          println(s"$x should be here")
          "two"
          }

          csNotify("four")


          So basically when you pass x in method , you have to give it for match as well.






          share|improve this answer































            3














            Amit Prasad's answer already shows how to fix it, but to explain the error message:



            {

            case org: String if !ignoredIds.contains(x) =>
            println( s" $x should not be here")
            "one"
            case org : String if ignoredIds.contains(x) =>
            println(s"$x should be here")
            "two"
            }


            on its own (without ... match before it) is a pattern-matching anonymous function, which can only be used where the compiler knows the argument type from the context, i.e. the expected type must be either PartialFunction[Something, SomethingElse] or a single-abstract-method type (including Something => SomethingElse).



            Here the expected type is String, which isn't either of those, so the compiler complains about not knowing what the argument type is.






            share|improve this answer

































              1














              You need to use match keyword here to use cases. There might be some value for which you will be using pattern matching. So use the following code in your function:



              x  match {
              case org: String if !ignoredIds.contains(x) => ???
              case org : String if ignoredIds.contains(x) => ???
              }


              Also, you should consider adding one more case which is default. As you know the parameter x of your function def csNotify(x: Any): String is of type any. So anything other than String can also be passed here like Int or Boolean or any custom type. In that case, the code will break with match error.



              There will also be a compiler warning saying match is not exhaustive as the current code does not handle all possible values for type Any of parameter x.



              But if you add one default case in your pattern matching, all the cases which are not handled by the first two cases (unexpected type or values) will go to the default case. In this way the code will be more robust:



              def csNotify(x : Any): String =  x  match {
              case org: String if !ignoredIds.contains(org) => ???
              case org : String if ignoredIds.contains(org) => ???
              case org => s"unwanted value: $org" // or any default value
              }


              Note: Kindly replace ??? with your intended code. :)






              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%2f53429809%2funderstanding-scala-default-argument-message%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









                4














                Your case is not finding the match against which it can check your block , and you have missed the match block:



                val ignoredIds = Array("one", "two", "three")


                def csNotify(x : Any): String = x match {

                case org: String if !ignoredIds.contains(x) =>
                println( s" $x should not be here")
                "one"
                case org : String if ignoredIds.contains(x) =>
                println(s"$x should be here")
                "two"
                }

                csNotify("four")


                So basically when you pass x in method , you have to give it for match as well.






                share|improve this answer




























                  4














                  Your case is not finding the match against which it can check your block , and you have missed the match block:



                  val ignoredIds = Array("one", "two", "three")


                  def csNotify(x : Any): String = x match {

                  case org: String if !ignoredIds.contains(x) =>
                  println( s" $x should not be here")
                  "one"
                  case org : String if ignoredIds.contains(x) =>
                  println(s"$x should be here")
                  "two"
                  }

                  csNotify("four")


                  So basically when you pass x in method , you have to give it for match as well.






                  share|improve this answer


























                    4












                    4








                    4







                    Your case is not finding the match against which it can check your block , and you have missed the match block:



                    val ignoredIds = Array("one", "two", "three")


                    def csNotify(x : Any): String = x match {

                    case org: String if !ignoredIds.contains(x) =>
                    println( s" $x should not be here")
                    "one"
                    case org : String if ignoredIds.contains(x) =>
                    println(s"$x should be here")
                    "two"
                    }

                    csNotify("four")


                    So basically when you pass x in method , you have to give it for match as well.






                    share|improve this answer













                    Your case is not finding the match against which it can check your block , and you have missed the match block:



                    val ignoredIds = Array("one", "two", "three")


                    def csNotify(x : Any): String = x match {

                    case org: String if !ignoredIds.contains(x) =>
                    println( s" $x should not be here")
                    "one"
                    case org : String if ignoredIds.contains(x) =>
                    println(s"$x should be here")
                    "two"
                    }

                    csNotify("four")


                    So basically when you pass x in method , you have to give it for match as well.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 11:27









                    Amit PrasadAmit Prasad

                    573315




                    573315

























                        3














                        Amit Prasad's answer already shows how to fix it, but to explain the error message:



                        {

                        case org: String if !ignoredIds.contains(x) =>
                        println( s" $x should not be here")
                        "one"
                        case org : String if ignoredIds.contains(x) =>
                        println(s"$x should be here")
                        "two"
                        }


                        on its own (without ... match before it) is a pattern-matching anonymous function, which can only be used where the compiler knows the argument type from the context, i.e. the expected type must be either PartialFunction[Something, SomethingElse] or a single-abstract-method type (including Something => SomethingElse).



                        Here the expected type is String, which isn't either of those, so the compiler complains about not knowing what the argument type is.






                        share|improve this answer






























                          3














                          Amit Prasad's answer already shows how to fix it, but to explain the error message:



                          {

                          case org: String if !ignoredIds.contains(x) =>
                          println( s" $x should not be here")
                          "one"
                          case org : String if ignoredIds.contains(x) =>
                          println(s"$x should be here")
                          "two"
                          }


                          on its own (without ... match before it) is a pattern-matching anonymous function, which can only be used where the compiler knows the argument type from the context, i.e. the expected type must be either PartialFunction[Something, SomethingElse] or a single-abstract-method type (including Something => SomethingElse).



                          Here the expected type is String, which isn't either of those, so the compiler complains about not knowing what the argument type is.






                          share|improve this answer




























                            3












                            3








                            3







                            Amit Prasad's answer already shows how to fix it, but to explain the error message:



                            {

                            case org: String if !ignoredIds.contains(x) =>
                            println( s" $x should not be here")
                            "one"
                            case org : String if ignoredIds.contains(x) =>
                            println(s"$x should be here")
                            "two"
                            }


                            on its own (without ... match before it) is a pattern-matching anonymous function, which can only be used where the compiler knows the argument type from the context, i.e. the expected type must be either PartialFunction[Something, SomethingElse] or a single-abstract-method type (including Something => SomethingElse).



                            Here the expected type is String, which isn't either of those, so the compiler complains about not knowing what the argument type is.






                            share|improve this answer















                            Amit Prasad's answer already shows how to fix it, but to explain the error message:



                            {

                            case org: String if !ignoredIds.contains(x) =>
                            println( s" $x should not be here")
                            "one"
                            case org : String if ignoredIds.contains(x) =>
                            println(s"$x should be here")
                            "two"
                            }


                            on its own (without ... match before it) is a pattern-matching anonymous function, which can only be used where the compiler knows the argument type from the context, i.e. the expected type must be either PartialFunction[Something, SomethingElse] or a single-abstract-method type (including Something => SomethingElse).



                            Here the expected type is String, which isn't either of those, so the compiler complains about not knowing what the argument type is.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 26 '18 at 17:44









                            Amit Prasad

                            573315




                            573315










                            answered Nov 22 '18 at 11:53









                            Alexey RomanovAlexey Romanov

                            108k26213354




                            108k26213354























                                1














                                You need to use match keyword here to use cases. There might be some value for which you will be using pattern matching. So use the following code in your function:



                                x  match {
                                case org: String if !ignoredIds.contains(x) => ???
                                case org : String if ignoredIds.contains(x) => ???
                                }


                                Also, you should consider adding one more case which is default. As you know the parameter x of your function def csNotify(x: Any): String is of type any. So anything other than String can also be passed here like Int or Boolean or any custom type. In that case, the code will break with match error.



                                There will also be a compiler warning saying match is not exhaustive as the current code does not handle all possible values for type Any of parameter x.



                                But if you add one default case in your pattern matching, all the cases which are not handled by the first two cases (unexpected type or values) will go to the default case. In this way the code will be more robust:



                                def csNotify(x : Any): String =  x  match {
                                case org: String if !ignoredIds.contains(org) => ???
                                case org : String if ignoredIds.contains(org) => ???
                                case org => s"unwanted value: $org" // or any default value
                                }


                                Note: Kindly replace ??? with your intended code. :)






                                share|improve this answer




























                                  1














                                  You need to use match keyword here to use cases. There might be some value for which you will be using pattern matching. So use the following code in your function:



                                  x  match {
                                  case org: String if !ignoredIds.contains(x) => ???
                                  case org : String if ignoredIds.contains(x) => ???
                                  }


                                  Also, you should consider adding one more case which is default. As you know the parameter x of your function def csNotify(x: Any): String is of type any. So anything other than String can also be passed here like Int or Boolean or any custom type. In that case, the code will break with match error.



                                  There will also be a compiler warning saying match is not exhaustive as the current code does not handle all possible values for type Any of parameter x.



                                  But if you add one default case in your pattern matching, all the cases which are not handled by the first two cases (unexpected type or values) will go to the default case. In this way the code will be more robust:



                                  def csNotify(x : Any): String =  x  match {
                                  case org: String if !ignoredIds.contains(org) => ???
                                  case org : String if ignoredIds.contains(org) => ???
                                  case org => s"unwanted value: $org" // or any default value
                                  }


                                  Note: Kindly replace ??? with your intended code. :)






                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    You need to use match keyword here to use cases. There might be some value for which you will be using pattern matching. So use the following code in your function:



                                    x  match {
                                    case org: String if !ignoredIds.contains(x) => ???
                                    case org : String if ignoredIds.contains(x) => ???
                                    }


                                    Also, you should consider adding one more case which is default. As you know the parameter x of your function def csNotify(x: Any): String is of type any. So anything other than String can also be passed here like Int or Boolean or any custom type. In that case, the code will break with match error.



                                    There will also be a compiler warning saying match is not exhaustive as the current code does not handle all possible values for type Any of parameter x.



                                    But if you add one default case in your pattern matching, all the cases which are not handled by the first two cases (unexpected type or values) will go to the default case. In this way the code will be more robust:



                                    def csNotify(x : Any): String =  x  match {
                                    case org: String if !ignoredIds.contains(org) => ???
                                    case org : String if ignoredIds.contains(org) => ???
                                    case org => s"unwanted value: $org" // or any default value
                                    }


                                    Note: Kindly replace ??? with your intended code. :)






                                    share|improve this answer













                                    You need to use match keyword here to use cases. There might be some value for which you will be using pattern matching. So use the following code in your function:



                                    x  match {
                                    case org: String if !ignoredIds.contains(x) => ???
                                    case org : String if ignoredIds.contains(x) => ???
                                    }


                                    Also, you should consider adding one more case which is default. As you know the parameter x of your function def csNotify(x: Any): String is of type any. So anything other than String can also be passed here like Int or Boolean or any custom type. In that case, the code will break with match error.



                                    There will also be a compiler warning saying match is not exhaustive as the current code does not handle all possible values for type Any of parameter x.



                                    But if you add one default case in your pattern matching, all the cases which are not handled by the first two cases (unexpected type or values) will go to the default case. In this way the code will be more robust:



                                    def csNotify(x : Any): String =  x  match {
                                    case org: String if !ignoredIds.contains(org) => ???
                                    case org : String if ignoredIds.contains(org) => ???
                                    case org => s"unwanted value: $org" // or any default value
                                    }


                                    Note: Kindly replace ??? with your intended code. :)







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 22 '18 at 11:55









                                    anuj saxenaanuj saxena

                                    24917




                                    24917






























                                        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%2f53429809%2funderstanding-scala-default-argument-message%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

                                        MongoDB - Not Authorized To Execute Command

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                        How to fix TextFormField cause rebuild widget in Flutter