You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#to_s and #inspect are commonly used methods defined on all objects in Crystal. They have a similar purpose, rendering a string representation of itself. Yet, their exact use cases are not precisely defined, especially what distinguishes them.
Returns a String representation of this object suitable to be embedded inside other expressions, sometimes providing more information about this object.
IMO that's not very helpful for defining these methods. A sloppy description like #to_s is for the user, #inspect for the developer would already be more descriptive.
In Python there are very similar methods __str__ (dubbed the "informal" representation) and __repr__ (the "official" representation).
The documentation for __repr__ suggests to make the output a valid Python expression that could be used to recreate the object with the same value, so that eval(repr(o)) == o. It also makes it clear, that this is not possible in all cases and asks to generally use a delimited, information-rich and unambiguous representation. A well-received SO answer puts it this way:
__repr__ goal is to be unambiguous.
__str__ goal is to be readable.
That sounds like a good basic definition, clearly differentiating the purposes. And it fits very well for what we use #to_s and #inspect for.
Adapted and rephrased to Crystal, this is my proposal for clarifying the definiton of these methods:
Object#to_s:
Returns a nicely readable and concise string representation of this object, typically intended for users.
Object#inspect:
Returns an unambigous and information-richt string representation of this object, typically intended for developers.
/cf #7525 for related discussion which led to the current wordings (improving the preceding state)
The text was updated successfully, but these errors were encountered:
This patch changes the method definitions as proposed in crystal-lang#9966 and adds some
additional notes.
The part about ambigouity of collection members is removed because that's not
generically relevant for `#inspect` but for individual implementations such as
`Array#inspect`. So I added a short note about it there.
It does not need to be repeated for every collection type.
#to_s
and#inspect
are commonly used methods defined on all objects in Crystal. They have a similar purpose, rendering a string representation of itself. Yet, their exact use cases are not precisely defined, especially what distinguishes them.Current definitions:
Object#to_s
:Object#inspect
:IMO that's not very helpful for defining these methods. A sloppy description like
#to_s
is for the user,#inspect
for the developer would already be more descriptive.In Python there are very similar methods
__str__
(dubbed the "informal" representation) and__repr__
(the "official" representation).The documentation for
__repr__
suggests to make the output a valid Python expression that could be used to recreate the object with the same value, so thateval(repr(o)) == o
. It also makes it clear, that this is not possible in all cases and asks to generally use a delimited, information-rich and unambiguous representation. A well-received SO answer puts it this way:__repr__
goal is to be unambiguous.__str__
goal is to be readable.That sounds like a good basic definition, clearly differentiating the purposes. And it fits very well for what we use
#to_s
and#inspect
for.Adapted and rephrased to Crystal, this is my proposal for clarifying the definiton of these methods:
Object#to_s
:Object#inspect
:/cf #7525 for related discussion which led to the current wordings (improving the preceding state)
The text was updated successfully, but these errors were encountered: