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

[stdlib] Add repr() function and Representable trait #2361

Closed

Conversation

gabrieldemarmiesse
Copy link
Contributor

@gabrieldemarmiesse gabrieldemarmiesse commented Apr 20, 2024

This PR is a follow up on the discussion there:

#2207

There is no RepresentableRaising trait yet, this can come later on.

We assume here that 42 is the Mojo representation of Int(42) because this is how it materializes. Same we assume that "hello" is the Mojo string representation of String("hello") because StringLiteral materializes into String.

We could have used Int(42) and String("hello") as Mojo representations but that seems very cumbersome and not aligned with the evolution of the language. The discussion is still open for other types.

The __repr__ implementation for String is very simplistic and I believe it's enough as a start, we can push the complexity in other separate PRs (handling \ and so on). I will open an issue when this PR is merged requesting contributor help to improve it.

I will create another issue after this PR is merged to track the conformance of (all?) library types to the Representable trait.

@jayzhan211
Copy link
Contributor

jayzhan211 commented Apr 21, 2024

Is Representable equivalent to Stringable mentioned in #2207?

Upd: It seems __repr__ is close to Debug in rust, and __str__ to Display in rust

Signed-off-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Signed-off-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Signed-off-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Signed-off-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
@JoeLoser JoeLoser added imported-internally Signals that a given pull request has been imported internally. merged-internally Indicates that this pull request has been merged internally labels May 1, 2024
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 1, 2024

✅🟣 This contribution has been merged 🟣✅

Hey @gabrieldemarmiesse,

Thanks so much again for the contribution! 🎉

We're moving to a new infrastructure for merging contributions to Mojo (we're using a tool called Copybara), and your contribution has now been merged into our internal copy of the Mojo Standard Library. I've added the "merged-internally" label on this PR.

The changes in this PR will appear here in the mojo repo nightly branch when we do our next outbound synchronization at the time that the next Mojo nightly is released. That should happen within the next 24 hours.

Please let me know if you have any questions or concerns.

@gabrieldemarmiesse
Copy link
Contributor Author

JoeLoser pushed a commit that referenced this pull request May 3, 2024
…#38803)

[External] [stdlib] Add `repr()` function and `Representable` trait

This PR is a follow up on the discussion there:

#2207

There is no `RepresentableRaising` trait yet, this can come later on.

We assume here that `42` is the Mojo representation of `Int(42)` because
this is how it materializes. Same we assume that `"hello"` is the Mojo
string representation of `String("hello")` because StringLiteral
materializes into String.

We could have used `Int(42)` and `String("hello")` as Mojo
representations but that seems very cumbersome and not aligned with the
evolution of the language. The discussion is still open for other types.

The `__repr__` implementation for String is very simplistic and I
believe it's enough as a start, we can push the complexity in other
separate PRs (handling `\` and so on). I will open an issue when this PR
is merged requesting contributor help to improve it.

I will create another issue after this PR is merged to track the
conformance of (all?) library types to the `Representable` trait.

Co-authored-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
Closes #2361
MODULAR_ORIG_COMMIT_REV_ID: 82c5e2b83582a61341b8e4a5bfa9d66f7c4f34ab
@JoeLoser
Copy link
Collaborator

JoeLoser commented May 3, 2024

🎉 🔥 This landed in tonight's nightly release in #2480!

Thanks again for the contribution!

@JoeLoser JoeLoser closed this May 3, 2024
@helehex
Copy link
Contributor

helehex commented May 3, 2024

another thing about repr, it takes non-printable characters and gives the representation of them, such as \n.
I already did this in one of my side projects, so I can add it to stdlib.

@JoeLoser JoeLoser added the merged-externally Merged externally in public mojo repo label May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants