Method signature for a function with default values











up vote
3
down vote

favorite












In Scala, is there a way to specify that a function should have default parameter values declared?



For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?



def helloName(name: String, greating: String = "hello"): Unit = { 
println(s"$greating $name")
}

def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}









share|improve this question




















  • 1




    A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
    – Jörg W Mittag
    Nov 9 at 13:49















up vote
3
down vote

favorite












In Scala, is there a way to specify that a function should have default parameter values declared?



For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?



def helloName(name: String, greating: String = "hello"): Unit = { 
println(s"$greating $name")
}

def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}









share|improve this question




















  • 1




    A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
    – Jörg W Mittag
    Nov 9 at 13:49













up vote
3
down vote

favorite









up vote
3
down vote

favorite











In Scala, is there a way to specify that a function should have default parameter values declared?



For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?



def helloName(name: String, greating: String = "hello"): Unit = { 
println(s"$greating $name")
}

def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}









share|improve this question















In Scala, is there a way to specify that a function should have default parameter values declared?



For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?



def helloName(name: String, greating: String = "hello"): Unit = { 
println(s"$greating $name")
}

def indirectHelloName(name: String, function: (String,String) => Unit): Unit = {
if (name == "Ted") {
function(name, "Custom Greeting for Ted!")
} else {
function(name) //This would use the default value for the second argument.
}
}






scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 23:12









jwvh

24.7k52038




24.7k52038










asked Nov 7 at 21:58









Mike Rylander

8,2761563112




8,2761563112








  • 1




    A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
    – Jörg W Mittag
    Nov 9 at 13:49














  • 1




    A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
    – Jörg W Mittag
    Nov 9 at 13:49








1




1




A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49




A function cannot have an optional parameter with default argument, therefore there is no way to specify one.
– Jörg W Mittag
Nov 9 at 13:49












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted











In Scala, is there a way to specify that a function should have default parameter values declared?



For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?




A function cannot have an optional parameter with default argument, therefore there is no way to specify one:



val f = (a: Int, b: Int) => a + b
//⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05

val g = (a: Int, b: Int = 5) => a + b
// <console>:1: error: ')' expected but '=' found.
// val g = (a: Int, b: Int = 5) => a + b
// ^

val h = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int) = a + b
}
//⇒ h: (Int, Int) => Int = <function2>

val i = new Function2[Int, Int, Int] {
override def apply(a: Int, b: Int = 5) = a + b
}
//⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>

i(3, 5)
//⇒ res: Int = 8

i(3)
// <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
// Unspecified value parameter v2.
// h(3)
// ^





share|improve this answer





















  • So, a method can have default values, but a function cannot? is that correct?
    – Mike Rylander
    Nov 16 at 16:32






  • 1




    Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
    – Jörg W Mittag
    Nov 16 at 21:51


















up vote
3
down vote













One thing you could do is move the default value from the parameter list to inside the method.



def helloName(in :String*) :Unit =
println(in.lift(1).getOrElse("hello") + ", " + in.head)

def indirectHelloName(name: String, function: (String*) => Unit): Unit =
if (name == "Ted") function(name, "Custom Greeting")
else function(name) //use default


usage:



indirectHelloName("Ted", helloName)  //Custom Greeting, Ted
indirectHelloName("Tam", helloName) //hello, Tam





share|improve this answer




























    up vote
    0
    down vote













    I tried and come up with the below answer



        def helloName(name: String, greeting: String = "hello"): Unit = {
    println(s"$greeting $name")
    }

    val helloNameFn = (x: String, y :String) => println(x + y)

    type fn = (String, String) => Unit

    def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
    if (name == "Ted") {
    function(name, "Custom Greeting for Ted!")
    } else {
    helloName(name) //This would use the default value for the second argument.
    }
    }

    helloNameFn("123", "123")


    Please try and let me know your comments.






    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',
      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%2f53198469%2fmethod-signature-for-a-function-with-default-values%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








      up vote
      1
      down vote



      accepted











      In Scala, is there a way to specify that a function should have default parameter values declared?



      For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?




      A function cannot have an optional parameter with default argument, therefore there is no way to specify one:



      val f = (a: Int, b: Int) => a + b
      //⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05

      val g = (a: Int, b: Int = 5) => a + b
      // <console>:1: error: ')' expected but '=' found.
      // val g = (a: Int, b: Int = 5) => a + b
      // ^

      val h = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int) = a + b
      }
      //⇒ h: (Int, Int) => Int = <function2>

      val i = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int = 5) = a + b
      }
      //⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>

      i(3, 5)
      //⇒ res: Int = 8

      i(3)
      // <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
      // Unspecified value parameter v2.
      // h(3)
      // ^





      share|improve this answer





















      • So, a method can have default values, but a function cannot? is that correct?
        – Mike Rylander
        Nov 16 at 16:32






      • 1




        Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
        – Jörg W Mittag
        Nov 16 at 21:51















      up vote
      1
      down vote



      accepted











      In Scala, is there a way to specify that a function should have default parameter values declared?



      For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?




      A function cannot have an optional parameter with default argument, therefore there is no way to specify one:



      val f = (a: Int, b: Int) => a + b
      //⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05

      val g = (a: Int, b: Int = 5) => a + b
      // <console>:1: error: ')' expected but '=' found.
      // val g = (a: Int, b: Int = 5) => a + b
      // ^

      val h = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int) = a + b
      }
      //⇒ h: (Int, Int) => Int = <function2>

      val i = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int = 5) = a + b
      }
      //⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>

      i(3, 5)
      //⇒ res: Int = 8

      i(3)
      // <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
      // Unspecified value parameter v2.
      // h(3)
      // ^





      share|improve this answer





















      • So, a method can have default values, but a function cannot? is that correct?
        – Mike Rylander
        Nov 16 at 16:32






      • 1




        Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
        – Jörg W Mittag
        Nov 16 at 21:51













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted







      In Scala, is there a way to specify that a function should have default parameter values declared?



      For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?




      A function cannot have an optional parameter with default argument, therefore there is no way to specify one:



      val f = (a: Int, b: Int) => a + b
      //⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05

      val g = (a: Int, b: Int = 5) => a + b
      // <console>:1: error: ')' expected but '=' found.
      // val g = (a: Int, b: Int = 5) => a + b
      // ^

      val h = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int) = a + b
      }
      //⇒ h: (Int, Int) => Int = <function2>

      val i = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int = 5) = a + b
      }
      //⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>

      i(3, 5)
      //⇒ res: Int = 8

      i(3)
      // <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
      // Unspecified value parameter v2.
      // h(3)
      // ^





      share|improve this answer













      In Scala, is there a way to specify that a function should have default parameter values declared?



      For example, in the code below, is there a way to specify in the signature of indirectHelloName that the provided function must provide a default value for the second parameter?




      A function cannot have an optional parameter with default argument, therefore there is no way to specify one:



      val f = (a: Int, b: Int) => a + b
      //⇒ f: (Int, Int) => Int = $$Lambda$1073/0x000000080070c840@6cd98a05

      val g = (a: Int, b: Int = 5) => a + b
      // <console>:1: error: ')' expected but '=' found.
      // val g = (a: Int, b: Int = 5) => a + b
      // ^

      val h = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int) = a + b
      }
      //⇒ h: (Int, Int) => Int = <function2>

      val i = new Function2[Int, Int, Int] {
      override def apply(a: Int, b: Int = 5) = a + b
      }
      //⇒ i: (Int, Int) => Int{def apply$default$2: Int @scala.annotation.unchecked.uncheckedVariance} = <function2>

      i(3, 5)
      //⇒ res: Int = 8

      i(3)
      // <console>:13: error: not enough arguments for method apply: (v1: Int, v2: Int)Int in trait Function2.
      // Unspecified value parameter v2.
      // h(3)
      // ^






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 10 at 12:01









      Jörg W Mittag

      287k62352545




      287k62352545












      • So, a method can have default values, but a function cannot? is that correct?
        – Mike Rylander
        Nov 16 at 16:32






      • 1




        Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
        – Jörg W Mittag
        Nov 16 at 21:51


















      • So, a method can have default values, but a function cannot? is that correct?
        – Mike Rylander
        Nov 16 at 16:32






      • 1




        Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
        – Jörg W Mittag
        Nov 16 at 21:51
















      So, a method can have default values, but a function cannot? is that correct?
      – Mike Rylander
      Nov 16 at 16:32




      So, a method can have default values, but a function cannot? is that correct?
      – Mike Rylander
      Nov 16 at 16:32




      1




      1




      Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
      – Jörg W Mittag
      Nov 16 at 21:51




      Yes. Methods can do lots of things that functions can't. Methods can be generic, have optional parameters with default arguments, have multiple parameter lists, have implicit parameter lists, be implicit themselves, and have overloads. Functions can do none of those things. You might ask yourself, why do we even have functions if they are so much worse than methods? Well, there is one very important thing that distinguishes functions from methods: functions are objects, methods aren't. And in an OO language, where everything you do is about objects, that is a big deal.
      – Jörg W Mittag
      Nov 16 at 21:51












      up vote
      3
      down vote













      One thing you could do is move the default value from the parameter list to inside the method.



      def helloName(in :String*) :Unit =
      println(in.lift(1).getOrElse("hello") + ", " + in.head)

      def indirectHelloName(name: String, function: (String*) => Unit): Unit =
      if (name == "Ted") function(name, "Custom Greeting")
      else function(name) //use default


      usage:



      indirectHelloName("Ted", helloName)  //Custom Greeting, Ted
      indirectHelloName("Tam", helloName) //hello, Tam





      share|improve this answer

























        up vote
        3
        down vote













        One thing you could do is move the default value from the parameter list to inside the method.



        def helloName(in :String*) :Unit =
        println(in.lift(1).getOrElse("hello") + ", " + in.head)

        def indirectHelloName(name: String, function: (String*) => Unit): Unit =
        if (name == "Ted") function(name, "Custom Greeting")
        else function(name) //use default


        usage:



        indirectHelloName("Ted", helloName)  //Custom Greeting, Ted
        indirectHelloName("Tam", helloName) //hello, Tam





        share|improve this answer























          up vote
          3
          down vote










          up vote
          3
          down vote









          One thing you could do is move the default value from the parameter list to inside the method.



          def helloName(in :String*) :Unit =
          println(in.lift(1).getOrElse("hello") + ", " + in.head)

          def indirectHelloName(name: String, function: (String*) => Unit): Unit =
          if (name == "Ted") function(name, "Custom Greeting")
          else function(name) //use default


          usage:



          indirectHelloName("Ted", helloName)  //Custom Greeting, Ted
          indirectHelloName("Tam", helloName) //hello, Tam





          share|improve this answer












          One thing you could do is move the default value from the parameter list to inside the method.



          def helloName(in :String*) :Unit =
          println(in.lift(1).getOrElse("hello") + ", " + in.head)

          def indirectHelloName(name: String, function: (String*) => Unit): Unit =
          if (name == "Ted") function(name, "Custom Greeting")
          else function(name) //use default


          usage:



          indirectHelloName("Ted", helloName)  //Custom Greeting, Ted
          indirectHelloName("Tam", helloName) //hello, Tam






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 22:39









          jwvh

          24.7k52038




          24.7k52038






















              up vote
              0
              down vote













              I tried and come up with the below answer



                  def helloName(name: String, greeting: String = "hello"): Unit = {
              println(s"$greeting $name")
              }

              val helloNameFn = (x: String, y :String) => println(x + y)

              type fn = (String, String) => Unit

              def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
              if (name == "Ted") {
              function(name, "Custom Greeting for Ted!")
              } else {
              helloName(name) //This would use the default value for the second argument.
              }
              }

              helloNameFn("123", "123")


              Please try and let me know your comments.






              share|improve this answer

























                up vote
                0
                down vote













                I tried and come up with the below answer



                    def helloName(name: String, greeting: String = "hello"): Unit = {
                println(s"$greeting $name")
                }

                val helloNameFn = (x: String, y :String) => println(x + y)

                type fn = (String, String) => Unit

                def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
                if (name == "Ted") {
                function(name, "Custom Greeting for Ted!")
                } else {
                helloName(name) //This would use the default value for the second argument.
                }
                }

                helloNameFn("123", "123")


                Please try and let me know your comments.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I tried and come up with the below answer



                      def helloName(name: String, greeting: String = "hello"): Unit = {
                  println(s"$greeting $name")
                  }

                  val helloNameFn = (x: String, y :String) => println(x + y)

                  type fn = (String, String) => Unit

                  def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
                  if (name == "Ted") {
                  function(name, "Custom Greeting for Ted!")
                  } else {
                  helloName(name) //This would use the default value for the second argument.
                  }
                  }

                  helloNameFn("123", "123")


                  Please try and let me know your comments.






                  share|improve this answer












                  I tried and come up with the below answer



                      def helloName(name: String, greeting: String = "hello"): Unit = {
                  println(s"$greeting $name")
                  }

                  val helloNameFn = (x: String, y :String) => println(x + y)

                  type fn = (String, String) => Unit

                  def indirectHelloName(name: String, function:fn = helloNameFn): Unit = {
                  if (name == "Ted") {
                  function(name, "Custom Greeting for Ted!")
                  } else {
                  helloName(name) //This would use the default value for the second argument.
                  }
                  }

                  helloNameFn("123", "123")


                  Please try and let me know your comments.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 7 at 22:20









                  Rajkumar Natarajan

                  1,0161133




                  1,0161133






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53198469%2fmethod-signature-for-a-function-with-default-values%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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini