Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
new IO API
Moonbit's current IO API
Currently, the most commonly used IO APIs of Moonbit is:
where:
Problems with current API
The current API calls
to_string
unconditionally, however, this would cause problems when printingString
itself:println((s : String))
to outputs
itselfderive(Show)
, we want the strings to be printed with double quotesTo have correct
println
onString
s,to_string
ofString
must return the string itself. To have correct derivedShow
implementation, we wantto_string
ofString
to add double quotes. These two requirements are contradictory.Worse, we cannot hack deriver of
Show
to treatString
specially. Because for types likestruct T[X] { x: X }
, we want their derivedShow
to work correctly whenX
is instantiated toString
. So the current IO API cannot fulfill both requirements (println
andShow
) at the same time.In essence,
String
serves two purpose in the language: as the final representation of things to be printed (don't want double quotes), and as a normal data type (need double quotes). The current API does not distinguish between these two uses ofString
, which is the cause of the problem.The proposed new IO API
Show
toDebug
to make its purpose clearer. Also rename the methodto_string
todebug_to_string
print
andprintln
should only acceptString
as argumentprintln
todebug
to avoid writing a lot ofprintln(bla.to_string())
boilerplates