Skip to content

Array.description uses debugDescription instead of description of its elements #67541

Open
@dnadoba

Description

@dnadoba

Given the following programs:

struct MyType {}

extension MyType: CustomStringConvertible {
    var description: String {
        "description of MyType"
    }
}

extension MyType: CustomDebugStringConvertible {
    var debugDescription: String {
        "debugDescription of MyType"
    }
}

@main
struct Main {
    static func main() async throws {
        print([MyType()])
        debugPrint([MyType()])
    }
}

this prints

[debugDescription of MyType]
[debugDescription of MyType]

but I would expect that the first print

[description of MyType]

This seems to be on purpose from the code comments in _makeCollectionDescription: https://github.com/apple/swift/blob/86265881c3a8c0480f94ebeedd67aef607d4a1f1/stdlib/public/core/ArrayShared.swift#L119
However, this is very unexpected and also probably quite slow as it looks like debugPrint cannot be specialised.

This also affects ArraySlice, ContiguousArray, Dictionary and Set.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ArrayArea → standard library: The `Array` typeCustomStringConvertibleArea → standard library: The `CustomStringConvertible` protocolbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrellaswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions