Wednesday, January 21, 2009

Scala Duck Typing

Last night I did a talk on Scala with David Pollak and Bill Venners for the San Fran JUG. This was a win-win situation for me. I got to talk about Scala, and I got to learn from David and Bill. One of the highlights for me was Bill's talk about how Scala allowed you to adopt techniques from dynamic languages like JavaScript, Ruby, and Python: monkey patching and duck typing. I knew some of this was possible, but did not understand it well until Bill explained it.

I made it a point to do some more reading on one of Scala's feature that enables duck typing, structural types. I found another great explanation of this by Debasish. Good stuff. If you look at the comments to Debasish's post, then you see a lot of debate about how using such technique can be harmful. I empathize with this sentiment.

I have worked with dynamic languages, particularly JavaScript and Ruby, where I look at the code and say "how in the hell does this work?" That question does not scale. If I have to give code to a multitude of other developers to work on and each constantly find themselves asking that question, then you fail. I don't buy the argument that you can succeed on a large scale with fewer, smarter programmers who are oh-so-clever enough to never have to ask that question.

However, I don't think you necessarily wind up with that question in Scala. Static typing once again comes to the rescue. Between Scala's static typing and desugaring, that question can be answered rather quickly. All you need is proper tooling. Right now the best Scala tools are all plugins for the major Java IDEs. This seems like a reasonable route, but at some point these plugins will need to do some things that you would never see in Java. For example, the debugger needs to be able to "make it obvious" what kind of implicit conversions are going on or how some object complies with a structural pattern. Better yet would be to provide this information at design time. Like if I typed 1 until 100 and I could get immediate insight about the implicit conversion of Int to RichInt for example.

3 comments:

Jorge Ortiz said...

You can use scalac with the -print option to see a de-sugared version of your source code.

Unknown said...

Exactly. That was what I was referring to when I said that the desugaring was (or could/should be) available to tools. I'd rather not admit how many times I've used both -print and good 'ol javap on Scala generated bytecode...

Mary Petter said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
Really very informative post you shared here. Kindly keep blogging.
If anyone wants to become a Front end developer learn from Javascript Online Training from India .
or learn thru JavaScript Online Training from India.
Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai