blob: b8062a12a61a2cc818177e4c1703ef5b6a41530f [file] [log] [blame]
//===--- StringInterpolation.swift.gyb - String Interpolation -*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from SwiftIntTypes import all_integer_types
# Number of bits in the Builtin.Word type
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
StreamableTypes = [
'String',
'Character',
'UnicodeScalar',
]
PrintableTypes = [
'Bool',
'Float32',
'Float64'
]
for int_ty in all_integer_types(word_bits):
PrintableTypes.append(int_ty.stdlib_name)
}%
extension String : _ExpressibleByStringInterpolation {
/// Creates a new string by concatenating the given interpolations.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you create a string using string interpolation. Instead, use string
/// interpolation to create a new string by including values, literals,
/// variables, or expressions enclosed in parentheses, prefixed by a
/// backslash (`\(`...`)`).
///
/// let price = 2
/// let number = 3
/// let message = "If one cookie costs \(price) dollars, " +
/// "\(number) cookies cost \(price * number) dollars."
/// print(message)
/// // Prints "If one cookie costs 2 dollars, 3 cookies cost 6 dollars."
@effects(readonly)
public init(stringInterpolation strings: String...) {
self.init()
for str in strings {
self += str
}
}
/// Creates a string containing the given expression's textual
/// representation.
///
/// Do not call this initializer directly. It is used by the compiler when
/// interpreting string interpolations.
///
/// - SeeAlso: `ExpressibleByStringInterpolation`
public init<T>(stringInterpolationSegment expr: T) {
self = String(describing: expr)
}
% for Type in StreamableTypes:
/// Creates a string containing the given value's textual representation.
///
/// Do not call this initializer directly. It is used by the compiler when
/// interpreting string interpolations.
///
/// - SeeAlso: `ExpressibleByStringInterpolation`
public init(stringInterpolationSegment expr: ${Type}) {
self = _toStringReadOnlyStreamable(expr)
}
% end
% for Type in PrintableTypes:
/// Creates a string containing the given value's textual representation.
///
/// Do not call this initializer directly. It is used by the compiler when
/// interpreting string interpolations.
///
/// - SeeAlso: `ExpressibleByStringInterpolation`
public init(stringInterpolationSegment expr: ${Type}) {
self = _toStringReadOnlyPrintable(expr)
}
% end
}
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: