Add ECMAScript 6 template string like support to Swift

Originator:davbeck
Number:rdar://20728777 Date Originated:28-Apr-2015 09:59 AM
Status:Open Resolved:
Product:Developer Tools Product Version:Swift 1.2
Classification:Enhancement Reproducible:Always
 
ECMAScript 6 adds a feature called template strings that behave similarly to StringInterpolationConvertible, but with a lot more flexibility. They use ticks (`) instead of quotes and when used without any modifiers, behave essentially exactly like StringInterpolationConvertible does in Swift. However, when you tag the string, it calls the function named the same as the tag. The function takes 2 arrays. The first being the string literal parts of the template, and the second being all the variables. The function can then combine the arguments in any way it sees fit. For instance, it could return an SQL statement, replacing the variables with question marks and keeping a reference to the arguments to pass to SQLite.

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

StringInterpolationConvertible however forces you to convert every part of the template to the given class/struct. Further, it does not distinguish between string literal components and the mixed in variables. This makes it difficult, if not impossible, to do anything with StringInterpolationConvertible besides create strings. With a more flexible implementation, things like NSPredicate could replace …WithFormat: methods with template literals.

Possible solutions:

protocol StringInterpolationConvertible {
    init<T>(stringInterpolation strings: [String], variables: [T])
}

protocol StringInterpolationConvertible {
    enum StringInterpolationPart<T> {
        case Literal(String)
        case Variable(T)
    }
    
    init<T>(stringInterpolation parts: [StringInterpolationPart<T>])
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!