Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smalltalk - printOn:

Tags:

smalltalk

I'm trying to figure out the point of printOn. I've looked through some classes that implement it, and it looks like it just helps to print the units for different datatypes. Is this accurate?

If so, could someone point me in the right direction of how to go about implementing this for my own class? I'm going to be doing arithmetic on potentially different unit sets, and would like to be able to have something like:

4 sec * 2 min = 8 sec min

The implementation to handle these units is complete, but errors keep getting thrown complaining that we have to implement our own printOn:

like image 770
MrDuk Avatar asked Feb 25 '26 12:02

MrDuk


2 Answers

printOn is like toString in Java, it add to a stream a string representation of an Object. You can look here to get a useful example.

like image 141
gma Avatar answered Mar 04 '26 00:03

gma


The printOn: method is actually intended to be used at development time to show enough information about the object to allow the developer to identify it. This prevents a lot of clicking and diving in inspectors and debuggers. It allows you to click on a variable in a list and see its value displayed in the text pane of the inspector or debugger.

It's not normally used for displaying strings to the end users. The problem there is that strings normally need to be internationalized and mapped into the appropriate language. If you want to display strings to the end user, there are better ways than using printOn:.

like image 45
David Buck Avatar answered Mar 03 '26 23:03

David Buck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!