Skip to content

[SR-920] Re-design builtin compiler protocols for literal convertible types #43532

@gribozavr

Description

@gribozavr
Previous ID SR-920
Radar rdar://problem/25100871
Original Reporter @gribozavr
Type Improvement
Additional Detail from JIRA
Votes 17
Component/s Standard Library
Labels Improvement, AffectsABI
Assignee None
Priority Medium

md5: 9c9daaa0076abc9937d022ed16def26e

is duplicated by:

  • SR-3317 Literal protocol for decimal literals should support precise decimal accuracy
  • SR-3970 Rework ExpressibleBy(Integer|Float)Literal protocols

relates to:

  • SR-7124 Double-rounding in floating-point literal conversion

Issue Description:

Swift aims to be an extensible language through the built-in compiler protocols, which allow user-defined types to adopt literal syntax.

If we want to commit to that design as ABI, we need to improve the protocols so that they are actually useful outside of the standard library.

1. There should be a type in the standard library that can represent the literal in a lossless way. For example, currently integer and floating point literals are truncated to the maximum precision supported by the library (Int64 and Float80) before they can be handled by user code.

For example, the library could provide an IntegerLiteral type, which wraps a 2048-bit integer that user types adopting the literal protocols could use. Same for floating-point literals (but the number should be represented exactly to allow decimal foating point literals).

2. Protocols related to string literals should have reasonable default implementations. Currently, to implement a string literal convertible type, you need to write many functions that just forward to each other to conform to the protocol.

3. When we get special transport types, the initializer labels can become simpler (e.g. drop the ‘literal’ from the label, since that would repeat type information).

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects ABIFlag: Affects ABIcompilerThe Swift compiler itselfstandard libraryArea: Standard library umbrellaswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposal

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions