SquareBracketAssociates / UpdatedPharoByExample

A version of Pharo by Example for Pharo 50.
Other
256 stars 41 forks source link

Proposal - subsection Duck Typing #196

Open bencoman opened 7 years ago

bencoman commented 7 years ago

Following on from my response here... http://forum.world.st/Question-tp4939264p4939265.html I propose a small subsection "Duck Typing" with an example like below. I don't see anywhere in PBE that this equivalence is explained...

On Sun, Mar 19, 2017 at 8:53 PM, Ben Coman btc@openinworld.com wrote:

In Playground, evaluate... Integer compile: ' plusOne ^ self + 1' .

Then inspect...

(1 2 3) collect: #plusOne.

Debug into it, to observe #value: sent to aBlock variable when it holds #plusOne such that ByteSymbol(Symbol)>>#value: twists this into a send of

plusOne (i.e. self) to anObject.

In general, since symbols and blocks both respond to #value: they can be used interchangeably.

Also perhaps a small summary of this discussion... http://forum.world.st/Smalltalk-strongly-typed-td4701894i20.html I'd seed the thought that Smalltalk is strongly typed, while pointing out lack of consensus but to not conflate static-typing and strongly-typed.

bencoman commented 7 years ago

"Duck typing" subsection might fit after "High-order iterators" subsection where #collect: is first mentioned. Or perhaps fit after "Every object is an instance of a class" subsection.