[swift-users] Any way to declare a method that suppresses the string interpolation warning?
Saagar Jha
saagar at saagarjha.com
Sun Apr 30 13:30:04 CDT 2017
Apologize for the late response, this message got buried in my inbox.
Saagar Jha
> On Apr 23, 2017, at 23:23, Rick Mann <rmann at latencyzero.com> wrote:
>
>>
>> On Apr 22, 2017, at 12:23 , Saagar Jha <saagar at saagarjha.com <mailto:saagar at saagarjha.com>> wrote:
>>
>>
>> Saagar Jha
>>
>>> On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>>
>>> I have a debugLog() method that looks like this:
>>>
>>> func
>>> debugLog<T>(_ inMsg: T, _ inFile : String = #file, _ inLine : Int = #line)
>>
>> Well, for starters, I don’t see why you need to make this function generic. Why not make inMsg an `Any?`?
>
> So I can write debugLog(<something other than string>)
Have you tried using `Any?`? You can pass in other stuff…
>
>>
>>> {
>>> let df = DateFormatter()
>>> df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
>>> let time = df.string(from: Date())
>>>
>>> let file = (inFile as NSString).lastPathComponent
>>> print("\(time) \(file):\(inLine) \(inMsg)”)
>>
>> Try \(inMsg ?? “nil”).
>
> No, this is missing the point. I don't want to have to write this everywhere. I just want to tell the compiler not to issue the warning in these cases, much in the way you can tell the compiler to check printf format specifiers.
The fundamental issue here is that printing an Optional is probably not what you want to do, since it will print Optional(“your wrapped value”). If this is what you want, you will need to be explicit with String(describing:); if not, then use the nil coalescing operator to fallback to a value you want. You can also try guaranteeing that the value is not an optional by unwrapping it.
>
>>
>>> }
>>>
>>> Is there any way to decorate it so that string interpolation of optionals passed to it inMsg don't produce the warning about using debugDescription? In the case of debug logging, that's completely acceptable, and I don't want to have to write String(describing:) everywhere.
>>>
>>>
>>> --
>>> Rick Mann
>>> rmann at latencyzero.com
>>>
>>>
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>
>
> --
> Rick Mann
> rmann at latencyzero.com <mailto:rmann at latencyzero.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170430/e6435813/attachment.html>
More information about the swift-users
mailing list