-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stdlib] Add
repr()
function and Representable
trait
Signed-off-by: gabrieldemarmiesse <gabrieldemarmiesse@gmail.com>
- Loading branch information
1 parent
6af6934
commit 09d667a
Showing
12 changed files
with
224 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
"""Provide the `repr` function. | ||
The functions and traits provided here are built-ins, so you don't need to import them. | ||
""" | ||
|
||
|
||
trait Representable: | ||
"""A trait that describes a type that has a String representation. | ||
Any type that conforms to the `Representable` trait can be used with the | ||
`repr` function. Any conforming type must also implement the `__repr__` method. | ||
Here is an example: | ||
```mojo | ||
@value | ||
struct Dog(Representable): | ||
var name: String | ||
var age: Int | ||
fn __repr__(self) -> String: | ||
return "Dog(name=" + repr(self.name) + ", age=" + repr(self.age) + ")" | ||
var dog = Dog("Rex", 5) | ||
print(repr(dog)) | ||
# Dog(name='Rex', age=5) | ||
``` | ||
The method `__repr__` should compute the "official" string representation of a type. | ||
If at all possible, this should look like a valid Mojo expression | ||
that could be used to recreate a struct instance with the same | ||
value (given an appropriate environment). | ||
So a returned String of the form `module_name.SomeStruct(arg1=value1, arg2=value2)` is advised. | ||
If this is not possible, a string of the form `<...some useful description...>` | ||
should be returned. | ||
The return value must be a `String` instance. | ||
This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. | ||
Note that when computing the string representation of a collection (`Dict`, `List`, `Set`, etc...), | ||
the `repr` function is called on each element, not the `str()` function. | ||
""" | ||
|
||
fn __repr__(self) -> String: | ||
"""Get the string representation of the type instance, if possible, compatible with Mojo syntax. | ||
Returns: | ||
The string representation of the instance. | ||
""" | ||
pass | ||
|
||
|
||
fn repr[T: Representable](value: T) -> String: | ||
""" | ||
Args: | ||
value: The value to get the string representation of. | ||
Parameters: | ||
T: The type of `value`. Must implement the `Representable` trait. | ||
Returns: | ||
The string representation of the given value. | ||
""" | ||
return value.__repr__() | ||
|
||
|
||
fn repr(value: None) -> String: | ||
"""Returns the string representation of `None`. | ||
Args: | ||
value: A `None` value. | ||
Returns: | ||
The string representation of `None`. | ||
""" | ||
return "None" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# RUN: %mojo-no-debug %s | ||
|
||
from testing import assert_equal | ||
|
||
|
||
@value | ||
struct Dog(Representable): | ||
var name: String | ||
var age: Int | ||
|
||
fn __repr__(self) -> String: | ||
return "Dog(name=" + repr(self.name) + ", age=" + repr(self.age) + ")" | ||
|
||
|
||
def test_explicit_conformance(): | ||
dog = Dog(name="Fido", age=3) | ||
assert_equal(repr(dog), "Dog(name='Fido', age=3)") | ||
|
||
|
||
@value | ||
struct Cat: | ||
var name: String | ||
var age: Int | ||
|
||
fn __repr__(self) -> String: | ||
return "Cat(name=" + repr(self.name) + ", age=" + repr(self.age) + ")" | ||
|
||
|
||
def test_implicit_conformance(): | ||
cat = Cat(name="Whiskers", age=2) | ||
assert_equal(repr(cat), "Cat(name='Whiskers', age=2)") | ||
|
||
|
||
def test_none_representation(): | ||
assert_equal(repr(None), "None") | ||
|
||
|
||
def main(): | ||
test_explicit_conformance() | ||
test_implicit_conformance() | ||
test_none_representation() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters