Skip to content

[SR-2303] ExpressibleByStringInterpolation: ability to restrict types used in string interpolation #44910

Closed
@jtbandes

Description

@jtbandes
Previous ID SR-2303
Radar None
Original Reporter @jtbandes
Type Improvement
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Improvement, LanguageFeatureRequest
Assignee @beccadax
Priority Medium

md5: d86f9995a0b9dc90aad8b59b81c4bab2

relates to:

  • SR-1260 ExpressibleByStringInterpolation: can't distinguish between literal components and String arguments
  • SR-3969 Improve ExpressibleByStringInterpolation

Issue Description:

It's desirable for types conforming to ExpressibleByStringInterpolation to be able to restrict the kinds of values that can be used in string interpolation.

The following example is a simplified version of brentdax (JIRA User)'s example from the swift-evolution thread:

// We want LocalizedString to be formed from a string interpolation;
// only LocalizedStringConvertible values should be allowed as interpolated values.

protocol LocalizedStringConvertible { ... }
struct LocalizedString { ... }

extension LocalizedString: ExpressibleByStringInterpolation {
    init(stringInterpolation segments: LocalizedString...) { }
    init(stringInterpolationSegment value: LocalizedStringConvertible) { ... }
    // no init<T>(stringInterpolationSegment:) is provided; only LocalizedStringConvertible values are valid
}

Metadata

Metadata

Assignees

Labels

compilerThe Swift compiler itselffeatureA feature request or implementationimprovementstandard libraryArea: Standard library umbrellaswift evolution implementedFlag → feature: A feature that was approved through the Swift evolution process and implemented

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions