Any way to declare a method that suppresses the string interpolation warning?


(Rick M) #1

I have a debugLog() method that looks like this:

func
debugLog<T>(_ inMsg: T, _ inFile : String = #file, _ inLine : Int = #line)
{
  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)")
}

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@latencyzero.com


(Saagar Jha) #2

Saagar Jha

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?`?

{
  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”).

···

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@swift.org> wrote:

}

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@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Rick M) #3

Saagar Jha

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>)

{
  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.

···

On Apr 22, 2017, at 12:23 , Saagar Jha <saagar@saagarjha.com> wrote:

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@swift.org> wrote:

}

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@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com


(Saagar Jha) #4

Apologize for the late response, this message got buried in my inbox.

Saagar Jha

Saagar Jha

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.

···

On Apr 23, 2017, at 23:23, Rick Mann <rmann@latencyzero.com> wrote:

On Apr 22, 2017, at 12:23 , Saagar Jha <saagar@saagarjha.com <mailto:saagar@saagarjha.com>> wrote:

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

}

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@latencyzero.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>


(David Sweeris) #5

IIRC, Swift doesn't include a mechanism for suppressing warnings because we don't want to encourage different "dialects" of the language. The best I can offer for ignoring it is something like:
extension CustomStringConvertible {
  // for "pretty print"
  var pp: String {
    return self.description
  }
}
extension Optional where Wrapped: CustomStringConvertible {
  var pp: String {
    return self?.description ?? "nil"
  }
}
(written on my phone... might contain errors)

That way at least it's quicker to type, cleaner to read, and the code is the same between Optional and non-Optional types.

- Dave Sweeris

···

On Apr 23, 2017, at 23:23, Rick Mann via swift-users <swift-users@swift.org> wrote:

On Apr 22, 2017, at 12:23 , Saagar Jha <saagar@saagarjha.com> wrote:

Saagar Jha

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@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>)

{
   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.


(Rick M) #6

Apologize for the late response, this message got buried in my inbox.

Saagar Jha

Saagar Jha

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…

That's probably fine. I just took it from some example somewhere.

{
  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.

What I'm trying to avoid is dealing with it at the call site. I have to do that every time, and for printing of debug messages, "Optional()" is fine (although I have a proposal in mind to address that, too; I'd much rather just see "nil")

···

On Apr 30, 2017, at 11:30 , Saagar Jha <saagar@saagarjha.com> wrote:

On Apr 23, 2017, at 23:23, Rick Mann <rmann@latencyzero.com> wrote:

On Apr 22, 2017, at 12:23 , Saagar Jha <saagar@saagarjha.com> wrote:

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@swift.org> wrote:

--
Rick Mann
rmann@latencyzero.com


(Saagar Jha) #7

Saagar Jha

Apologize for the late response, this message got buried in my inbox.

Saagar Jha

Saagar Jha

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…

That's probably fine. I just took it from some example somewhere.

{
  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.

What I'm trying to avoid is dealing with it at the call site. I have to do that every time, and for printing of debug messages, "Optional()" is fine (although I have a proposal in mind to address that, too; I'd much rather just see "nil”)

Hmm, your code doesn’t seem to have any warnings anymore…

···

On May 1, 2017, at 14:08, Rick Mann <rmann@latencyzero.com> wrote:

On Apr 30, 2017, at 11:30 , Saagar Jha <saagar@saagarjha.com> wrote:

On Apr 23, 2017, at 23:23, Rick Mann <rmann@latencyzero.com> wrote:

On Apr 22, 2017, at 12:23 , Saagar Jha <saagar@saagarjha.com> wrote:

On Apr 21, 2017, at 04:35, Rick Mann via swift-users <swift-users@swift.org> wrote:

--
Rick Mann
rmann@latencyzero.com <mailto:rmann@latencyzero.com>