[Proposal] Replace String.init(repeating repeatedValue: Character, count: Int) and String.init(repeating repeatedValue: UnicodeScalar, count: Int) by String.init(repeating repeatedValue: String, count: Int)

Hi,

I’d like to propose some improvements for String initializers and String.append methods. It is addressing [SR-1964] String(count:, repeatedValue:) and String.append() are ambiguous without an explicit cast to Character · Issue #44573 · apple/swift · GitHub (rdar://problem/17028332 <rdar://problem/17028332>).

# Replace String.init(repeating repeatedValue: Character, count: Int) and String.init(repeating repeatedValue: UnicodeScalar, count: Int) by String.init(repeating repeatedValue: String, count: Int)

* Proposal:
* Author: [Roman Levenstein](http://github.com/swiftix\)
* Status:
* Review manager:

Introduction

···

=========

This proposal suggest replacing String initializers taking Character or UnicodeScalar as a repeating value by a more general initializer that takes a String as a repeating value. This is done to avoid the ambiguities in the current String API, which can be only resolved by explicit casting. It is also proposed to remove one of the String.append APIs to match these changes.

All user-facing Swift APIs must go through Swift Evolution. While this is a relatively simple API change with an existing implementation, this formal proposal provides a paper trail as is normal and usual for this process.

Motivation

This change introduces a non-ambiguous API for constructing Strings. With the set of String initializers available today, ones often needs to explicitly cast the repeating value literal to disambiguate what initializer is meant to be used.

An example of the ambiguity:

let x = String(repeating:"0", count: 10)

error: repl.swift:29:9: error: ambiguous use of 'init(repeating:count:)'
let x = String(repeating:"0", count: 10)
        ^

Swift.String:11:12: note: found this candidate
    public init(repeating repeatedValue: Character, count: Int)
           ^

Swift.String:21:12: note: found this candidate
    public init(repeating repeatedValue: UnicodeScalar, count: Int)
           ^

To disambiguate, one currently needs to write something like:
`let zeroes = String(repeating: "0" as Character, count: 10)` or
`let zeroes = String(repeating: "0" as UnicodeScalar, count: 10)`

Detailed Design

This update affects `String`.

It is proposed to replace the following ambiguous API:
* `public init(repeating repeatedValue: Character, count: Int)`
* `public init(repeating repeatedValue: UnicodeScalar, count: Int)`

by the following, more powerful API:
* `public init(repeating repeatedValue: String, count: Int)`

To match this change, it is also proposed to remove the following String.append API:
* `public mutating func append(_ x: UnicodeScalar)`
because there is already an existing and more powerful API:
* `public mutating func append(_ other: String)`

Impact on Existing Code

Existing third party code using these to be removed String APIs will need migration.
A fixit could be provided to automate this migration.

Alternatives Considered

Not Applicable

-Roman

Hi,

I’d like to propose some improvements for String initializers and
String.append methods. It is addressing
[SR-1964] String(count:, repeatedValue:) and String.append() are ambiguous without an explicit cast to Character · Issue #44573 · apple/swift · GitHub
<Issues · apple/swift-issues · GitHub; (rdar://problem/17028332
<rdar://problem/17028332>).

+1; let's do it!

···

on Thu Jul 21 2016, Roman Levenstein <swift-evolution@swift.org> wrote:

# Replace String.init(repeating repeatedValue: Character, count: Int) and String.init(repeating repeatedValue: UnicodeScalar, count: Int) by String.init(repeating repeatedValue: String, count: Int)

* Proposal:
* Author: [Roman Levenstein](http://github.com/swiftix\)
* Status:
* Review manager:

Introduction

This proposal suggest replacing String initializers taking Character or UnicodeScalar as a repeating value by a more general initializer that takes a String as a repeating value. This is done to avoid the ambiguities in the current String API, which can be only resolved by explicit casting. It is also proposed to remove one of the String.append APIs to match these changes.

All user-facing Swift APIs must go through Swift Evolution. While this is a relatively simple API change with an existing implementation, this formal proposal provides a paper trail as is normal and usual for this process.

Motivation

This change introduces a non-ambiguous API for constructing Strings. With the set of String initializers available today, ones often needs to explicitly cast the repeating value literal to disambiguate what initializer is meant to be used.

An example of the ambiguity:

let x = String(repeating:"0", count: 10)

error: repl.swift:29:9: error: ambiguous use of 'init(repeating:count:)'
let x = String(repeating:"0", count: 10)
        ^

Swift.String:11:12: note: found this candidate
    public init(repeating repeatedValue: Character, count: Int)
           ^

Swift.String:21:12: note: found this candidate
    public init(repeating repeatedValue: UnicodeScalar, count: Int)
           ^

To disambiguate, one currently needs to write something like:
`let zeroes = String(repeating: "0" as Character, count: 10)` or
`let zeroes = String(repeating: "0" as UnicodeScalar, count: 10)`

Detailed Design

This update affects `String`.

It is proposed to replace the following ambiguous API:
* `public init(repeating repeatedValue: Character, count: Int)`
* `public init(repeating repeatedValue: UnicodeScalar, count: Int)`

by the following, more powerful API:
* `public init(repeating repeatedValue: String, count: Int)`

To match this change, it is also proposed to remove the following String.append API:
* `public mutating func append(_ x: UnicodeScalar)`
because there is already an existing and more powerful API:
* `public mutating func append(_ other: String)`

Impact on Existing Code

Existing third party code using these to be removed String APIs will need migration.
A fixit could be provided to automate this migration.

Alternatives Considered

Not Applicable

-Roman

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Dave

It's because there is no character literal!
something like 'c','\xxxx' in language C.

在 2016年7月22日,00:54,Roman Levenstein via swift-evolution <swift-evolution@swift.org<mailto:swift-evolution@swift.org>> 写道:

Hi,

I’d like to propose some improvements for String initializers and String.append methods. It is addressing [SR-1964] String(count:, repeatedValue:) and String.append() are ambiguous without an explicit cast to Character · Issue #44573 · apple/swift · GitHub (rdar://problem/17028332).

# Replace String.init(repeating repeatedValue: Character, count: Int) and String.init(repeating repeatedValue: UnicodeScalar, count: Int) by String.init(repeating repeatedValue: String, count: Int)

* Proposal:
* Author: [Roman Levenstein](http://github.com/swiftix\)
* Status:
* Review manager:

Introduction

···

=========

This proposal suggest replacing String initializers taking Character or UnicodeScalar as a repeating value by a more general initializer that takes a String as a repeating value. This is done to avoid the ambiguities in the current String API, which can be only resolved by explicit casting. It is also proposed to remove one of the String.append APIs to match these changes.

All user-facing Swift APIs must go through Swift Evolution. While this is a relatively simple API change with an existing implementation, this formal proposal provides a paper trail as is normal and usual for this process.

Motivation

This change introduces a non-ambiguous API for constructing Strings. With the set of String initializers available today, ones often needs to explicitly cast the repeating value literal to disambiguate what initializer is meant to be used.

An example of the ambiguity:

let x = String(repeating:"0", count: 10)

error: repl.swift:29:9: error: ambiguous use of 'init(repeating:count:)'
let x = String(repeating:"0", count: 10)
        ^

Swift.String:11:12: note: found this candidate
    public init(repeating repeatedValue: Character, count: Int)
           ^

Swift.String:21:12: note: found this candidate
    public init(repeating repeatedValue: UnicodeScalar, count: Int)
           ^

To disambiguate, one currently needs to write something like:
`let zeroes = String(repeating: "0" as Character, count: 10)` or
`let zeroes = String(repeating: "0" as UnicodeScalar, count: 10)`

Detailed Design

This update affects `String`.

It is proposed to replace the following ambiguous API:
* `public init(repeating repeatedValue: Character, count: Int)`
* `public init(repeating repeatedValue: UnicodeScalar, count: Int)`

by the following, more powerful API:
* `public init(repeating repeatedValue: String, count: Int)`

To match this change, it is also proposed to remove the following String.append API:
* `public mutating func append(_ x: UnicodeScalar)`
because there is already an existing and more powerful API:
* `public mutating func append(_ other: String)`

Impact on Existing Code

Existing third party code using these to be removed String APIs will need migration.
A fixit could be provided to automate this migration.

Alternatives Considered

Not Applicable

-Roman

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org<mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution