How to get the expected value of #function or #line?












0















I have a function that contains #function and #line that returns a string:



func exportMessage(content: String) -> String {
if let fileURL = URL(string: #file) {
return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
}

return "(#function) - Line: (#line)n(content)"
}


When trying to use exportMessage above function in another function:



func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}


The output is:




MyPlayground.playground: exportMessage(content:) - Line: 5



instance is nil




It contains "exportMessage" but not "doSomething" as the name of the function (#function). It is the same for the line number, it is line number in exportMessage() but not in doSomething().



My expectation is to get the function name and the line number for doSomething(), as:




MyPlayground.playground: doSomething() - Line: 12



instance is nil




Since I am aiming to use exportMessage in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?










share|improve this question





























    0















    I have a function that contains #function and #line that returns a string:



    func exportMessage(content: String) -> String {
    if let fileURL = URL(string: #file) {
    return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
    }

    return "(#function) - Line: (#line)n(content)"
    }


    When trying to use exportMessage above function in another function:



    func doSomething() {
    let result = exportMessage(content: "instance is nil")
    print(result)
    }


    The output is:




    MyPlayground.playground: exportMessage(content:) - Line: 5



    instance is nil




    It contains "exportMessage" but not "doSomething" as the name of the function (#function). It is the same for the line number, it is line number in exportMessage() but not in doSomething().



    My expectation is to get the function name and the line number for doSomething(), as:




    MyPlayground.playground: doSomething() - Line: 12



    instance is nil




    Since I am aiming to use exportMessage in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?










    share|improve this question



























      0












      0








      0








      I have a function that contains #function and #line that returns a string:



      func exportMessage(content: String) -> String {
      if let fileURL = URL(string: #file) {
      return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
      }

      return "(#function) - Line: (#line)n(content)"
      }


      When trying to use exportMessage above function in another function:



      func doSomething() {
      let result = exportMessage(content: "instance is nil")
      print(result)
      }


      The output is:




      MyPlayground.playground: exportMessage(content:) - Line: 5



      instance is nil




      It contains "exportMessage" but not "doSomething" as the name of the function (#function). It is the same for the line number, it is line number in exportMessage() but not in doSomething().



      My expectation is to get the function name and the line number for doSomething(), as:




      MyPlayground.playground: doSomething() - Line: 12



      instance is nil




      Since I am aiming to use exportMessage in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?










      share|improve this question
















      I have a function that contains #function and #line that returns a string:



      func exportMessage(content: String) -> String {
      if let fileURL = URL(string: #file) {
      return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
      }

      return "(#function) - Line: (#line)n(content)"
      }


      When trying to use exportMessage above function in another function:



      func doSomething() {
      let result = exportMessage(content: "instance is nil")
      print(result)
      }


      The output is:




      MyPlayground.playground: exportMessage(content:) - Line: 5



      instance is nil




      It contains "exportMessage" but not "doSomething" as the name of the function (#function). It is the same for the line number, it is line number in exportMessage() but not in doSomething().



      My expectation is to get the function name and the line number for doSomething(), as:




      MyPlayground.playground: doSomething() - Line: 12



      instance is nil




      Since I am aiming to use exportMessage in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?







      swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 17:17







      Ahmad F

















      asked Nov 21 '18 at 17:07









      Ahmad FAhmad F

      16.3k104181




      16.3k104181
























          2 Answers
          2






          active

          oldest

          votes


















          2














          What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething? Why not the caller of doSomething? Or the caller of the caller? Ultimately, if this is how #function and friends behaved, it would only ever print start from libdyld.dylid.



          To achieve the behaviour you want, you have to manually pull in the #function and #line values from the parent context:



          func exportMessage(
          content: String,
          callingFunction: String = #function,
          callingLine: Int = #line
          ) -> String {
          if let fileURL = URL(string: callingFunction) {
          return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
          }

          return "(callingFunction) - Line: (callingLine)n(content)"
          }

          func doSomething() {
          let result = exportMessage(content: "instance is nil")
          print(result)
          }





          share|improve this answer
























          • That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

            – Ahmad F
            Nov 22 '18 at 8:06





















          1














          Use #function and #line as default values in the method. Default values are evaluated at the point of call.



          func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
          if let fileURL = URL(string: file) {
          return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
          }

          return "(function) - Line: (line)n(content)"
          }





          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%2f53417249%2fhow-to-get-the-expected-value-of-function-or-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething? Why not the caller of doSomething? Or the caller of the caller? Ultimately, if this is how #function and friends behaved, it would only ever print start from libdyld.dylid.



            To achieve the behaviour you want, you have to manually pull in the #function and #line values from the parent context:



            func exportMessage(
            content: String,
            callingFunction: String = #function,
            callingLine: Int = #line
            ) -> String {
            if let fileURL = URL(string: callingFunction) {
            return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
            }

            return "(callingFunction) - Line: (callingLine)n(content)"
            }

            func doSomething() {
            let result = exportMessage(content: "instance is nil")
            print(result)
            }





            share|improve this answer
























            • That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

              – Ahmad F
              Nov 22 '18 at 8:06


















            2














            What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething? Why not the caller of doSomething? Or the caller of the caller? Ultimately, if this is how #function and friends behaved, it would only ever print start from libdyld.dylid.



            To achieve the behaviour you want, you have to manually pull in the #function and #line values from the parent context:



            func exportMessage(
            content: String,
            callingFunction: String = #function,
            callingLine: Int = #line
            ) -> String {
            if let fileURL = URL(string: callingFunction) {
            return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
            }

            return "(callingFunction) - Line: (callingLine)n(content)"
            }

            func doSomething() {
            let result = exportMessage(content: "instance is nil")
            print(result)
            }





            share|improve this answer
























            • That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

              – Ahmad F
              Nov 22 '18 at 8:06
















            2












            2








            2







            What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething? Why not the caller of doSomething? Or the caller of the caller? Ultimately, if this is how #function and friends behaved, it would only ever print start from libdyld.dylid.



            To achieve the behaviour you want, you have to manually pull in the #function and #line values from the parent context:



            func exportMessage(
            content: String,
            callingFunction: String = #function,
            callingLine: Int = #line
            ) -> String {
            if let fileURL = URL(string: callingFunction) {
            return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
            }

            return "(callingFunction) - Line: (callingLine)n(content)"
            }

            func doSomething() {
            let result = exportMessage(content: "instance is nil")
            print(result)
            }





            share|improve this answer













            What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething? Why not the caller of doSomething? Or the caller of the caller? Ultimately, if this is how #function and friends behaved, it would only ever print start from libdyld.dylid.



            To achieve the behaviour you want, you have to manually pull in the #function and #line values from the parent context:



            func exportMessage(
            content: String,
            callingFunction: String = #function,
            callingLine: Int = #line
            ) -> String {
            if let fileURL = URL(string: callingFunction) {
            return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
            }

            return "(callingFunction) - Line: (callingLine)n(content)"
            }

            func doSomething() {
            let result = exportMessage(content: "instance is nil")
            print(result)
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 17:25









            AlexanderAlexander

            31.2k54878




            31.2k54878













            • That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

              – Ahmad F
              Nov 22 '18 at 8:06





















            • That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

              – Ahmad F
              Nov 22 '18 at 8:06



















            That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

            – Ahmad F
            Nov 22 '18 at 8:06







            That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!

            – Ahmad F
            Nov 22 '18 at 8:06















            1














            Use #function and #line as default values in the method. Default values are evaluated at the point of call.



            func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
            if let fileURL = URL(string: file) {
            return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
            }

            return "(function) - Line: (line)n(content)"
            }





            share|improve this answer




























              1














              Use #function and #line as default values in the method. Default values are evaluated at the point of call.



              func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
              if let fileURL = URL(string: file) {
              return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
              }

              return "(function) - Line: (line)n(content)"
              }





              share|improve this answer


























                1












                1








                1







                Use #function and #line as default values in the method. Default values are evaluated at the point of call.



                func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
                if let fileURL = URL(string: file) {
                return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
                }

                return "(function) - Line: (line)n(content)"
                }





                share|improve this answer













                Use #function and #line as default values in the method. Default values are evaluated at the point of call.



                func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
                if let fileURL = URL(string: file) {
                return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
                }

                return "(function) - Line: (line)n(content)"
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 17:25









                Rob NapierRob Napier

                200k28294421




                200k28294421






























                    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%2f53417249%2fhow-to-get-the-expected-value-of-function-or-line%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

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin