mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
We don't have a way yet to say "this is deprecated for users, but let the stdlib use it without complaining" so we need to do refactoring shenanigans.
99 lines
3.0 KiB
Swift
99 lines
3.0 KiB
Swift
//===--- StringInterpolation.swift.gyb - String Interpolation -*- swift -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://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:
|