Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Created March 28, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humblehacker/23d323e30f580925d54a34900622f926 to your computer and use it in GitHub Desktop.
Save humblehacker/23d323e30f580925d54a34900622f926 to your computer and use it in GitHub Desktop.
// Based on https://github.com/T-Pham/NoOptionalInterpolation
import Foundation
public
protocol Unwrappable
{
func unwrap() -> Any?
}
extension Optional: Unwrappable
{
public
func unwrap() -> Any?
{
switch self
{
case nil:
return nil
case let unwrappable as Unwrappable:
return unwrappable.unwrap()
case let any:
return any
}
}
}
public
extension String
{
public
init<T:Unwrappable>(stringInterpolationSegment expr: T)
{
guard let unwrapped = expr.unwrap() else { self.init("nil")!; return }
self.init(reflecting: unwrapped)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment