Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions builtin/autoloc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub(all) type SourceLoc
fn SourceLoc::repr(self : Self) -> String = "%loc_to_string"

///|
pub impl Show for SourceLoc with output(self, logger) {
logger.write_string(self.repr())
pub impl Show for SourceLoc with to_string(self) {
self.repr()
}

///|
Expand Down
4 changes: 2 additions & 2 deletions builtin/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ pub fn Double::to_string(self : Double) -> String {
// }

///|
pub impl Show for Double with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for Double with to_string(self) {
Double::to_string(self)
}

///|
Expand Down
3 changes: 2 additions & 1 deletion builtin/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,9 @@ pub impl Shl for UInt
pub impl Shl for UInt16
pub impl Shl for UInt64

#must_implement_one(output, to_string)
pub(open) trait Show {
output(Self, &Logger) -> Unit
output(Self, &Logger) -> Unit = _
to_string(Self) -> String = _
}
pub impl Show for Unit
Expand Down
34 changes: 17 additions & 17 deletions builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,47 @@
// limitations under the License.

///|
pub impl Show for Unit with output(_self, logger) {
logger.write_string("()")
pub impl Show for Unit with to_string(_self) {
"()"
}

///|
pub impl Show for Bool with output(self, logger) {
pub impl Show for Bool with to_string(self) {
if self {
logger.write_string("true")
"true"
} else {
logger.write_string("false")
"false"
}
}

///|
pub impl Show for Int with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for Int with to_string(self) {
Int::to_string(self)
}

///|
pub impl Show for Int64 with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for Int64 with to_string(self) {
Int64::to_string(self)
}

///|
pub impl Show for UInt with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for UInt with to_string(self) {
UInt::to_string(self)
}

///|
pub impl Show for UInt64 with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for UInt64 with to_string(self) {
UInt64::to_string(self)
}

///|
pub impl Show for Byte with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for Byte with to_string(self) {
Byte::to_string(self)
}

///|
pub impl Show for UInt16 with output(self, logger) {
logger.write_string(self.to_string())
pub impl Show for UInt16 with to_string(self) {
UInt16::to_string(self)
}

///|
Expand Down
16 changes: 13 additions & 3 deletions builtin/traits.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,32 @@ impl Logger with write_char(self, value) {

///|
/// Trait for types that can be converted to `String`
#must_implement_one(output, to_string)
pub(open) trait Show {
// `output` is used for composition of aggregate structure.
// `output` writes a string representation of `self` to a logger.
// `output` should produce a valid MoonBit-syntax representation if possible.
// For example, `Show::output` for `String` should be quoted
output(Self, &Logger) -> Unit
// By default `output` is implemented using `to_string`,
// but some types, such as `String` and `Char`, may override `output`,
// for different (escaped) behavior when composed into larger structures.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is no longer the case? we have two purely for efficiency?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It depends on how we are going to phase out the transition.

output(Self, &Logger) -> Unit = _
// `to_string` should be used by end users of `Show`,
// for printing, interpolation, etc. only, and should not be used for composition.
// By default `to_string` is implemented using `output` and a buffer,
// but some types, such as `String`, may override `to_string`,
// for different (unescaped) behavior when interpolated/printed directly
// for different (unescaped) behavior when interpolated/printed directly.
to_string(Self) -> String = _
}

///|
/// Default implementation for `Show::to_string`, uses a `StringBuilder`
/// Default implementation for `Show::output`, uses `Show::to_string`.
impl Show with output(self, logger) {
logger.write_string(self.to_string())
}

///|
/// Default implementation for `Show::to_string`, uses a `StringBuilder`.
impl Show with to_string(self) {
let logger = StringBuilder::new()
self.output(logger)
Expand Down
Loading