Does Your Programming Language Ever Surprise You in a Good Way? | MediumSitemapOpen in appSign up<br>Sign in
Medium Logo
Get app<br>Write
Search
Sign up<br>Sign in
Does Your Programming Language Ever Surprise You in a Good Way?
Donald Raab
3 min read·<br>1 hour ago
Listen
Share
What does two divided by three return in your programming language?
Press enter or click to view image in full size
Photo by Claudio Schwarz on Unsplash
When I learned Smalltalk in the 1990s, I experienced a surprise that no other programming language I had learned up to that point prepared me for.<br>What is behind door 2 / 3?
The answer to this question varies by programming language. Smalltalk changes the script. The answer to this question in Smalltalk is a Fraction.
Press enter or click to view image in full size
In Smalltalk, the result of 2 / 3 is a Fraction
Wut the?
The real surprise as it turns out is not that Smalltalk has support for fractions. That was the initial surprise. The real surprise is how / works. The / in Smalltalk is a binary message sent to the SmallInteger 2 with the parameter 3, which is also a SmallInteger. Smalltalk is dynamically typed, so the return type is not required to be an SmallInteger.<br>This is the implementation of / as defined on SmallInteger. You can see the other types that define / as well here.
Press enter or click to view image in full size
Finding the implementors of /, and browsing the definition of / for SmallInteger.
Now an interesting surprise here is the definition of / for Collection. Let’s divide two arrays and see what we get back.
Press enter or click to view image in full size
Dividing two arrays results in the results stored in a third array
Now let’s divide a single array with a SmallInteger.
Press enter or click to view image in full size
Dividing an array with a SmallInteger
Now let’s flip it and divide the SmallInteger by the array.
Press enter or click to view image in full size
Dividing a SmallInteger by an array
Now let’s make my final point, about being surprised by your programming language.
Press enter or click to view image in full size
Combining two arrays into an array of Points
Writing this blog was a bit surprising for me. I wasn’t sure where I was going to go with this. I knew I would start with Fractions, because those are surprising enough in a programming language. But I didn’t expect to be doing math with collections. We explored just a bit about binary messages. If it isn’t obvious, you can define binary messages on your own objects. You might refer to this as operator overloading, but that’s a kind of dismissive way to think about what Smalltalk is doing here.<br>The whole of Smalltalk is defined as sending messages to objects. Here we send the message / to some object. Either the object responds with a an appropriate result to the message, or responds that it does not understand the message. In this regard, a / is no different than any other message, with the exception that binary messages like / can only have one parameter.<br>I hope this exploration was a nice surprise for you. It was for me.<br>Thanks for reading!<br>Note: I used the open source Pharo Smalltalk 13.0 for these code examples.<br>I am the creator of and committer for the Eclipse Collections OSS project, which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions. I am the author of the book, Eclipse Collections Categorically: Level up your programming game.
Smalltalk
Programming
Open Source
Object Oriented
Written by Donald Raab
2.5K followers<br>·340 following
Java Champion. Creator of the Eclipse Collections OSS Java library (https://github.com/eclipse-collections). Inspired by Smalltalk. Opinions are my own.
Help
Status
About
Careers
Press
Blog
Store
Privacy
Rules
Terms
Text to speech