opendylan icon indicating copy to clipboard operation
opendylan copied to clipboard

Merge common-dylan:format and io:format

Open cgay opened this issue 7 years ago • 2 comments

This drives me crazy and it's not just cosmetic. I just spent a(nother) half hour debugging why my print-object method wasn't being used only to find that it's because format calls condition-to-string on <error> instances and because condition-to-string is define in common-dylan it uses the common-dylan formatter, which doesn't know about print-object (I assume) because it doesn't know about streams (I assume).

One hack that could be an interim measure is to export %set-formatter-function-if-you-really-know-what-youre-doing from common-dylan and have the IO library call it. At least this way there would be a unified formatting protocol if you use the IO library.

But we should figure out a better way to deal with this. One thing that really strikes me is how easy this is in Go: just define a String() method on your type and done. Or in Python, just define __str__ and/or __repr__ methods on your class. If we did something similar then maybe format could be fully defined within common-dylan?

cgay avatar Nov 17 '18 00:11 cgay

In my code, user-visible conditions are a separate subclass of <format-string-condition> and I wrote a condition-to-string method on them that calls the io library's format-to-string method with the condition's format string and arguments. That's the cleanest thing to do without reworking the libraries.

You can't tag a format string as being for the io:format or common-dylan:simple-format module, so you just have to know which one the format string is intended for and going to get dispatched to.

As for what Go or Python do, that's something of a red herring. In the case of conditions, at least, you can of course have condition-to-string return a string just like they do, but how are you going to construct that string with parameters? You've got to use one of the format-to-string versions.

BarAgent avatar Nov 18 '18 12:11 BarAgent

It is exactly having to come up with workarounds like that that I would like to avoid. I want to rework the libraries and make life better for newcomers (and for myself).

...but how are you going to construct that string with parameters?

By using common-dylan:format, which is not going to give a shit about streams.

cgay avatar Nov 22 '18 06:11 cgay