Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit conversions #820

Merged
merged 17 commits into from
Sep 21, 2021
Merged

Implicit conversions #820

merged 17 commits into from
Sep 21, 2021

Conversation

zygoloid
Copy link
Contributor

@zygoloid zygoloid commented Sep 8, 2021

Proposal to support a limited set of implicit conversions. This would generally permit only implicit conversions that are lossless and semantics-preserving. In particular, this proposal allows:

  • Conversion from an integer type to a wider integer type of the same signedness, and from an unsigned integer type to a wider signed integer type.
  • Conversion from an integer type to a floating-point type that has enough mantissa bits to exactly represent all integers in the source type.
  • Conversion from integer literals to integer and floating-point types that can represent them.
  • Conversion from floating-point literals to floating-point types that can represent them.
  • Conversions required for generics: conversions of values between facet types, and conversions of types between type-of-types, as described in the generics proposals.
  • Conversions required for inheritance: derived-to-base conversions for class pointers and class values.

Other conversions, such as lossy conversions between arithmetic types and conversions between bool and other types are not supported.

A mechanism to permit user-defined types to support implicit conversion is provided:

class StringView { ... }
impl String as ImplicitAs(StringView) {
  fn Convert[me: Self]() -> StringView { ... }
}
fn F(s: StringView);
fn G(s: String) {
  // OK, calls F(s.(ImplicitAs(StringView).Convert)())
  F(s);
}

@zygoloid zygoloid added the proposal A proposal label Sep 8, 2021
@zygoloid zygoloid added this to Draft in Proposals via automation Sep 8, 2021
@zygoloid zygoloid requested a review from a team September 8, 2021 20:09
@google-cla google-cla bot added the cla: yes PR meets CLA requirements according to bot. label Sep 8, 2021
@zygoloid zygoloid marked this pull request as ready for review September 10, 2021 02:15
@zygoloid zygoloid requested a review from a team as a code owner September 10, 2021 02:15
docs/design/generics/details.md Show resolved Hide resolved
proposals/p0820.md Outdated Show resolved Hide resolved
proposals/p0820.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
zygoloid and others added 2 commits September 10, 2021 18:37
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
can for an unsigned type.

We get a sign bit for free in each floating-point type, which covers
all but the lowest value in iN, and the lowest value is always exactly
representable because it's -2^M for a sufficiently small M.
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really like this overall BTW. Also somewhat happy to defer most of the detailed review to @josh11b. =] A few minor wording / clarification comments inline, nothing super substantial though.

docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
docs/design/expressions/implicit_conversions.md Outdated Show resolved Hide resolved
@chandlerc chandlerc moved this from Draft to RFC in Proposals Sep 15, 2021
@github-actions github-actions bot added the proposal rfc Proposal with request-for-comment sent out label Sep 15, 2021
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still some useful iteration I think on lossless and semantically the same... happy to chat in person to try to reach a good solution there...


- `T` -> `U` if `T` is equivalent to `U`

Two types are equivalent if they can be used interchangeably, implicitly: they
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phrase "used interchangably" seems a bit confusing here. The subsequent explanation is clear, but maybe just jump to that?

Suggested change
Two types are equivalent if they can be used interchangeably, implicitly: they
Two types are equivalent if they

Co-authored-by: Geoff Romer <gromer@google.com>
Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, ship it. I think this has converged enough, and if we get more issues that need to be clarified, I think we can fix this forward and still unblock subsequent design work.

@zygoloid zygoloid merged commit 8cd5e96 into carbon-language:trunk Sep 21, 2021
Proposals automation moved this from RFC to Accepted Sep 21, 2021
@github-actions github-actions bot added proposal accepted Decision made, proposal accepted and removed proposal rfc Proposal with request-for-comment sent out labels Sep 21, 2021
@zygoloid zygoloid deleted the proposal-implicit-conversions branch March 11, 2022 01:02
chandlerc added a commit that referenced this pull request Jun 28, 2022
Proposal to support a limited set of implicit conversions.

This would generally permit only implicit conversions that are lossless and semantics-preserving. In particular, this proposal allows:

-   Conversion from an integer type to a wider integer type of the same signedness, and from an unsigned integer type to a wider signed integer type.
-   Conversion from an integer type to a floating-point type that has enough mantissa bits to exactly represent all integers in the source type.
-   Conversion from integer literals to integer and floating-point types that can represent them.
-   Conversion from floating-point literals to floating-point types that can represent them.
-   Conversions required for generics: conversions of values between facet types, and conversions of types between type-of-types, as described in the generics proposals.
-   Conversions required for inheritance: derived-to-base conversions for class pointers and class values.

Other conversions, such as lossy conversions between arithmetic types and conversions between bool and other types are not supported.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Geoff Romer <gromer@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot. proposal accepted Decision made, proposal accepted proposal A proposal
Projects
No open projects
Proposals
Accepted
Development

Successfully merging this pull request may close these issues.

None yet

4 participants