Reputation: 115
i am reading swift from apple docs and learning about statements. but couldnot find any information about the Line Control Statements.
According to the docs
A line control statement is used to specify a line number and filename that can be different from the line number and filename of the source code being compiled. Use a line control statement to change the source code location used by Swift for diagnostic and debugging purposes.
A line control statement has the following forms:
#sourceLocation(file: filename, line: line number) #sourceLocation()
My question is when should i use it? The docs lags an example about the topic.Any links or some hints would be helpful.
Upvotes: 2
Views: 389
Reputation: 437422
Unfortunately, #sourceLocation
seriously diminishes its utility because it only accepts string literal for the file
and integer literal for the line
. In the revision discussion to SE-0034, it was suggested that #sourceLocation
“is not a widely visible end-user feature”.
I really wish it accepted a normal string and integer values (e.g., that I could capture #file
and #line
, and pass that to #sourceLocation
). That would be extremely useful. E.g., Xcode 15 has a feature that lets you jump from a OSLog
or Logger
message in the console to a line of code in your project, and this feature honors #sourceLocation
. Right now, all my logging messages take me to my logging utility library rather than to the #file
and #line
that originated the logging message. A squandered opportunity, IMHO.
Bottom line, #sourceLocation
has very limited utility given the inflexible implementation.
Upvotes: 1
Reputation: 124997
This isn't the sort of thing you'd ever need as a beginner, and you could probably go through an entire career without using it. It seems to be meant for use in tools that generate source code. See the comments in the original feature proposal for the complete story.
TL/DR: Don't worry about it, you'll never need it.
Upvotes: 4