How to pass a function as an optional parameter Swift












-2















New to Swift and I am currently getting around passing a function as an optional parameter in the following way:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc()
{
myFunc(callBack: dummy())
}

func myFunc(callBack: (Void))
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


i.e. exploiting polymorphism



This seems like an inelegant way to do this, is there a better way,



Thanks,
Tom



edit:



I am aware I could shorten this by doing:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc(callBack: (Void) = dummy())
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


But I would still say this is an inelegant solution










share|improve this question




















  • 1





    (Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

    – user28434
    Nov 20 '18 at 11:44











  • @Moritz Noted and fixed

    – thoxey
    Nov 20 '18 at 11:58
















-2















New to Swift and I am currently getting around passing a function as an optional parameter in the following way:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc()
{
myFunc(callBack: dummy())
}

func myFunc(callBack: (Void))
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


i.e. exploiting polymorphism



This seems like an inelegant way to do this, is there a better way,



Thanks,
Tom



edit:



I am aware I could shorten this by doing:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc(callBack: (Void) = dummy())
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


But I would still say this is an inelegant solution










share|improve this question




















  • 1





    (Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

    – user28434
    Nov 20 '18 at 11:44











  • @Moritz Noted and fixed

    – thoxey
    Nov 20 '18 at 11:58














-2












-2








-2








New to Swift and I am currently getting around passing a function as an optional parameter in the following way:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc()
{
myFunc(callBack: dummy())
}

func myFunc(callBack: (Void))
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


i.e. exploiting polymorphism



This seems like an inelegant way to do this, is there a better way,



Thanks,
Tom



edit:



I am aware I could shorten this by doing:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc(callBack: (Void) = dummy())
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


But I would still say this is an inelegant solution










share|improve this question
















New to Swift and I am currently getting around passing a function as an optional parameter in the following way:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc()
{
myFunc(callBack: dummy())
}

func myFunc(callBack: (Void))
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


i.e. exploiting polymorphism



This seems like an inelegant way to do this, is there a better way,



Thanks,
Tom



edit:



I am aware I could shorten this by doing:



import Foundation 

func foo()
{
print("Foo")
}

func bar()
{
print("Bar")
}

func dummy()
{
return
}

func myFunc(callBack: (Void) = dummy())
{
foo()
callBack
}

myFunc()
myFunc(callBack: bar())


But I would still say this is an inelegant solution







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 11:57







thoxey

















asked Nov 20 '18 at 11:05









thoxeythoxey

416




416








  • 1





    (Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

    – user28434
    Nov 20 '18 at 11:44











  • @Moritz Noted and fixed

    – thoxey
    Nov 20 '18 at 11:58














  • 1





    (Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

    – user28434
    Nov 20 '18 at 11:44











  • @Moritz Noted and fixed

    – thoxey
    Nov 20 '18 at 11:58








1




1





(Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

– user28434
Nov 20 '18 at 11:44





(Void) doesn't make sence here, function type that accept nothing and returns nothing would be () -> Void.

– user28434
Nov 20 '18 at 11:44













@Moritz Noted and fixed

– thoxey
Nov 20 '18 at 11:58





@Moritz Noted and fixed

– thoxey
Nov 20 '18 at 11:58












4 Answers
4






active

oldest

votes


















2














You can easily define a default value for a function as parameter:



func foo(completion: () -> Void = { }) {
completion()
}


You could also have it be nil by default:



func foo(completion: (() -> Void)? = nil) {


When it's an optional closure, you'll have to call it like this:



completion?()





share|improve this answer


























  • I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

    – kabanus
    Nov 20 '18 at 11:50













  • It seems like it is, yes. Thanks!

    – LinusGeffarth
    Nov 20 '18 at 11:50











  • This is what I was after, seems to be more "elegant" in my opinion, thanks!

    – thoxey
    Nov 20 '18 at 11:53



















2














Your are referring to default arguments:




You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.




So you need something like:



func myfunc (callback: ()->void = dummy) {


LinusGeffarth notes that callbacks in Swift are called completion, or closure as avismara notes.






share|improve this answer


























  • Not exactly, see the added snippet above, default and optional parameters are not the same thing

    – thoxey
    Nov 20 '18 at 11:17








  • 1





    In swift, callbacks are usually called completion.

    – LinusGeffarth
    Nov 20 '18 at 11:37








  • 1





    @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

    – kabanus
    Nov 20 '18 at 11:43






  • 1





    Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

    – LinusGeffarth
    Nov 20 '18 at 11:45








  • 2





    My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

    – avismara
    Nov 20 '18 at 11:48



















1














Umm, are you passing function as an optional parameter, though? It looks like you have written a method that accepts Void, you have a function that accepts Void and you are just calling that method. I don't see any trace of polymorphism here except probably that the myFunc has multiple signatures. This method is an inelegant solution not because it is inelegant, but because it isn't a solution.



Here is a correct example of polymorphism in functional systems:



func printA() {
print("A")
}

func printB() {
print("B")
}

//This method accepts a function as a parameter
func higherOrderPrinter(with print: () -> Void ) {
print() //Can be anything, depends on the method you send here. Polymorphism much? :)
}

higherOrderPrinter(with: printA) //Prints A. See carefully how I am NOT doing printA()
higherOrderPrinter(with: printB) //Prints B
//In fact...
higherOrderPrinter {
print("C")
} //Prints C





share|improve this answer

































    0














    Full code with answer applied:



    import Foundation 

    func foo()
    {
    print("Foo")
    }

    func bar()
    {
    print("Bar")
    }

    func myFunc(completion: () -> Void = { })
    {
    foo()
    completion()
    }

    myFunc()
    myFunc(completion: bar)





    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%2f53391622%2fhow-to-pass-a-function-as-an-optional-parameter-swift%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can easily define a default value for a function as parameter:



      func foo(completion: () -> Void = { }) {
      completion()
      }


      You could also have it be nil by default:



      func foo(completion: (() -> Void)? = nil) {


      When it's an optional closure, you'll have to call it like this:



      completion?()





      share|improve this answer


























      • I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

        – kabanus
        Nov 20 '18 at 11:50













      • It seems like it is, yes. Thanks!

        – LinusGeffarth
        Nov 20 '18 at 11:50











      • This is what I was after, seems to be more "elegant" in my opinion, thanks!

        – thoxey
        Nov 20 '18 at 11:53
















      2














      You can easily define a default value for a function as parameter:



      func foo(completion: () -> Void = { }) {
      completion()
      }


      You could also have it be nil by default:



      func foo(completion: (() -> Void)? = nil) {


      When it's an optional closure, you'll have to call it like this:



      completion?()





      share|improve this answer


























      • I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

        – kabanus
        Nov 20 '18 at 11:50













      • It seems like it is, yes. Thanks!

        – LinusGeffarth
        Nov 20 '18 at 11:50











      • This is what I was after, seems to be more "elegant" in my opinion, thanks!

        – thoxey
        Nov 20 '18 at 11:53














      2












      2








      2







      You can easily define a default value for a function as parameter:



      func foo(completion: () -> Void = { }) {
      completion()
      }


      You could also have it be nil by default:



      func foo(completion: (() -> Void)? = nil) {


      When it's an optional closure, you'll have to call it like this:



      completion?()





      share|improve this answer















      You can easily define a default value for a function as parameter:



      func foo(completion: () -> Void = { }) {
      completion()
      }


      You could also have it be nil by default:



      func foo(completion: (() -> Void)? = nil) {


      When it's an optional closure, you'll have to call it like this:



      completion?()






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 '18 at 11:50

























      answered Nov 20 '18 at 11:48









      LinusGeffarthLinusGeffarth

      8,8001454100




      8,8001454100













      • I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

        – kabanus
        Nov 20 '18 at 11:50













      • It seems like it is, yes. Thanks!

        – LinusGeffarth
        Nov 20 '18 at 11:50











      • This is what I was after, seems to be more "elegant" in my opinion, thanks!

        – thoxey
        Nov 20 '18 at 11:53



















      • I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

        – kabanus
        Nov 20 '18 at 11:50













      • It seems like it is, yes. Thanks!

        – LinusGeffarth
        Nov 20 '18 at 11:50











      • This is what I was after, seems to be more "elegant" in my opinion, thanks!

        – thoxey
        Nov 20 '18 at 11:53

















      I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

      – kabanus
      Nov 20 '18 at 11:50







      I would not have guessed that is what you meant at all. Anyway, +1 since I think this maybe the "elegant" thing OP wants.

      – kabanus
      Nov 20 '18 at 11:50















      It seems like it is, yes. Thanks!

      – LinusGeffarth
      Nov 20 '18 at 11:50





      It seems like it is, yes. Thanks!

      – LinusGeffarth
      Nov 20 '18 at 11:50













      This is what I was after, seems to be more "elegant" in my opinion, thanks!

      – thoxey
      Nov 20 '18 at 11:53





      This is what I was after, seems to be more "elegant" in my opinion, thanks!

      – thoxey
      Nov 20 '18 at 11:53













      2














      Your are referring to default arguments:




      You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.




      So you need something like:



      func myfunc (callback: ()->void = dummy) {


      LinusGeffarth notes that callbacks in Swift are called completion, or closure as avismara notes.






      share|improve this answer


























      • Not exactly, see the added snippet above, default and optional parameters are not the same thing

        – thoxey
        Nov 20 '18 at 11:17








      • 1





        In swift, callbacks are usually called completion.

        – LinusGeffarth
        Nov 20 '18 at 11:37








      • 1





        @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

        – kabanus
        Nov 20 '18 at 11:43






      • 1





        Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

        – LinusGeffarth
        Nov 20 '18 at 11:45








      • 2





        My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

        – avismara
        Nov 20 '18 at 11:48
















      2














      Your are referring to default arguments:




      You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.




      So you need something like:



      func myfunc (callback: ()->void = dummy) {


      LinusGeffarth notes that callbacks in Swift are called completion, or closure as avismara notes.






      share|improve this answer


























      • Not exactly, see the added snippet above, default and optional parameters are not the same thing

        – thoxey
        Nov 20 '18 at 11:17








      • 1





        In swift, callbacks are usually called completion.

        – LinusGeffarth
        Nov 20 '18 at 11:37








      • 1





        @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

        – kabanus
        Nov 20 '18 at 11:43






      • 1





        Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

        – LinusGeffarth
        Nov 20 '18 at 11:45








      • 2





        My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

        – avismara
        Nov 20 '18 at 11:48














      2












      2








      2







      Your are referring to default arguments:




      You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.




      So you need something like:



      func myfunc (callback: ()->void = dummy) {


      LinusGeffarth notes that callbacks in Swift are called completion, or closure as avismara notes.






      share|improve this answer















      Your are referring to default arguments:




      You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.




      So you need something like:



      func myfunc (callback: ()->void = dummy) {


      LinusGeffarth notes that callbacks in Swift are called completion, or closure as avismara notes.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 '18 at 11:49

























      answered Nov 20 '18 at 11:09









      kabanuskabanus

      11.6k31339




      11.6k31339













      • Not exactly, see the added snippet above, default and optional parameters are not the same thing

        – thoxey
        Nov 20 '18 at 11:17








      • 1





        In swift, callbacks are usually called completion.

        – LinusGeffarth
        Nov 20 '18 at 11:37








      • 1





        @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

        – kabanus
        Nov 20 '18 at 11:43






      • 1





        Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

        – LinusGeffarth
        Nov 20 '18 at 11:45








      • 2





        My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

        – avismara
        Nov 20 '18 at 11:48



















      • Not exactly, see the added snippet above, default and optional parameters are not the same thing

        – thoxey
        Nov 20 '18 at 11:17








      • 1





        In swift, callbacks are usually called completion.

        – LinusGeffarth
        Nov 20 '18 at 11:37








      • 1





        @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

        – kabanus
        Nov 20 '18 at 11:43






      • 1





        Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

        – LinusGeffarth
        Nov 20 '18 at 11:45








      • 2





        My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

        – avismara
        Nov 20 '18 at 11:48

















      Not exactly, see the added snippet above, default and optional parameters are not the same thing

      – thoxey
      Nov 20 '18 at 11:17







      Not exactly, see the added snippet above, default and optional parameters are not the same thing

      – thoxey
      Nov 20 '18 at 11:17






      1




      1





      In swift, callbacks are usually called completion.

      – LinusGeffarth
      Nov 20 '18 at 11:37







      In swift, callbacks are usually called completion.

      – LinusGeffarth
      Nov 20 '18 at 11:37






      1




      1





      @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

      – kabanus
      Nov 20 '18 at 11:43





      @LinusGeffarth I am not sure. I dislike turning answers into a tutorial session, except maybe some specific cases of well researched homework. I am also unsure how much tutoring you are exactly expecting - I suggest you provide your own answer instead if mine is insufficient - you can elaborate on best practices if you see fit there.

      – kabanus
      Nov 20 '18 at 11:43




      1




      1





      Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

      – LinusGeffarth
      Nov 20 '18 at 11:45







      Adding another answer is a little redundant... Indeed, OP hasn't done (any) research, apparently, because there are tons of answers on completion handlers. Anyway, I think an answer isn't really an answer if OP's trying to find a specific line, yet the answer only provides their code again.

      – LinusGeffarth
      Nov 20 '18 at 11:45






      2




      2





      My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

      – avismara
      Nov 20 '18 at 11:48





      My two cents: In Swift they aren't really called completion -- they sometimes are based on the context -- but in general they are called closures. :)

      – avismara
      Nov 20 '18 at 11:48











      1














      Umm, are you passing function as an optional parameter, though? It looks like you have written a method that accepts Void, you have a function that accepts Void and you are just calling that method. I don't see any trace of polymorphism here except probably that the myFunc has multiple signatures. This method is an inelegant solution not because it is inelegant, but because it isn't a solution.



      Here is a correct example of polymorphism in functional systems:



      func printA() {
      print("A")
      }

      func printB() {
      print("B")
      }

      //This method accepts a function as a parameter
      func higherOrderPrinter(with print: () -> Void ) {
      print() //Can be anything, depends on the method you send here. Polymorphism much? :)
      }

      higherOrderPrinter(with: printA) //Prints A. See carefully how I am NOT doing printA()
      higherOrderPrinter(with: printB) //Prints B
      //In fact...
      higherOrderPrinter {
      print("C")
      } //Prints C





      share|improve this answer






























        1














        Umm, are you passing function as an optional parameter, though? It looks like you have written a method that accepts Void, you have a function that accepts Void and you are just calling that method. I don't see any trace of polymorphism here except probably that the myFunc has multiple signatures. This method is an inelegant solution not because it is inelegant, but because it isn't a solution.



        Here is a correct example of polymorphism in functional systems:



        func printA() {
        print("A")
        }

        func printB() {
        print("B")
        }

        //This method accepts a function as a parameter
        func higherOrderPrinter(with print: () -> Void ) {
        print() //Can be anything, depends on the method you send here. Polymorphism much? :)
        }

        higherOrderPrinter(with: printA) //Prints A. See carefully how I am NOT doing printA()
        higherOrderPrinter(with: printB) //Prints B
        //In fact...
        higherOrderPrinter {
        print("C")
        } //Prints C





        share|improve this answer




























          1












          1








          1







          Umm, are you passing function as an optional parameter, though? It looks like you have written a method that accepts Void, you have a function that accepts Void and you are just calling that method. I don't see any trace of polymorphism here except probably that the myFunc has multiple signatures. This method is an inelegant solution not because it is inelegant, but because it isn't a solution.



          Here is a correct example of polymorphism in functional systems:



          func printA() {
          print("A")
          }

          func printB() {
          print("B")
          }

          //This method accepts a function as a parameter
          func higherOrderPrinter(with print: () -> Void ) {
          print() //Can be anything, depends on the method you send here. Polymorphism much? :)
          }

          higherOrderPrinter(with: printA) //Prints A. See carefully how I am NOT doing printA()
          higherOrderPrinter(with: printB) //Prints B
          //In fact...
          higherOrderPrinter {
          print("C")
          } //Prints C





          share|improve this answer















          Umm, are you passing function as an optional parameter, though? It looks like you have written a method that accepts Void, you have a function that accepts Void and you are just calling that method. I don't see any trace of polymorphism here except probably that the myFunc has multiple signatures. This method is an inelegant solution not because it is inelegant, but because it isn't a solution.



          Here is a correct example of polymorphism in functional systems:



          func printA() {
          print("A")
          }

          func printB() {
          print("B")
          }

          //This method accepts a function as a parameter
          func higherOrderPrinter(with print: () -> Void ) {
          print() //Can be anything, depends on the method you send here. Polymorphism much? :)
          }

          higherOrderPrinter(with: printA) //Prints A. See carefully how I am NOT doing printA()
          higherOrderPrinter(with: printB) //Prints B
          //In fact...
          higherOrderPrinter {
          print("C")
          } //Prints C






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 11:44

























          answered Nov 20 '18 at 11:19









          avismaraavismara

          3,72622648




          3,72622648























              0














              Full code with answer applied:



              import Foundation 

              func foo()
              {
              print("Foo")
              }

              func bar()
              {
              print("Bar")
              }

              func myFunc(completion: () -> Void = { })
              {
              foo()
              completion()
              }

              myFunc()
              myFunc(completion: bar)





              share|improve this answer




























                0














                Full code with answer applied:



                import Foundation 

                func foo()
                {
                print("Foo")
                }

                func bar()
                {
                print("Bar")
                }

                func myFunc(completion: () -> Void = { })
                {
                foo()
                completion()
                }

                myFunc()
                myFunc(completion: bar)





                share|improve this answer


























                  0












                  0








                  0







                  Full code with answer applied:



                  import Foundation 

                  func foo()
                  {
                  print("Foo")
                  }

                  func bar()
                  {
                  print("Bar")
                  }

                  func myFunc(completion: () -> Void = { })
                  {
                  foo()
                  completion()
                  }

                  myFunc()
                  myFunc(completion: bar)





                  share|improve this answer













                  Full code with answer applied:



                  import Foundation 

                  func foo()
                  {
                  print("Foo")
                  }

                  func bar()
                  {
                  print("Bar")
                  }

                  func myFunc(completion: () -> Void = { })
                  {
                  foo()
                  completion()
                  }

                  myFunc()
                  myFunc(completion: bar)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 11:57









                  thoxeythoxey

                  416




                  416






























                      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%2f53391622%2fhow-to-pass-a-function-as-an-optional-parameter-swift%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

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents