Mark Thomas

Deceptively random thoughts

Type safe duck typing with Scala

without comments

If it walks like a duck and quacks like a duck, I would call it a duck.

Duck typing uses the set of methods and properties of an object, rather than its position in a type hierarchy to decide what you can do with it.

Scala’s structural typing feature provides something close to duck typing in a type safe way. A structural type is close to a .NET anonymous type; a type with a set of methods and properties but with no name. An example in Scala based on one of Wikipedia’s examples of duck typing:

class Duck {
  def quack = println("Quaaaaaack !")
  def feathers = println("The duck has white and gray feathers.")
}
 
class Person {
  def quack = println("The person imitates a duck.")
  def feathers = println("The person takes a feather from the ground and shows it.")
}
 
def inTheForest(duck: { def quack; def feathers }) = {
  duck.quack
  duck.feathers
}

The inTheForest function declares that it will accept any object that has a quack and feathers method, both of which take no parameters and return Unit (like void in Java/C#). When executed, the code works as expected:

scala> inTheForest(new Person)
The person imitates a duck.
The person takes a feather from the ground and shows it.

scala> inTheForest(new Duck)
Quaaaaaack !
The duck has white and gray feathers.

To demonstrate the type safety, lets try using a string:

scala> inTheForest("Duck")
:6: error: type mismatch;
 found   : java.lang.String("Duck")
 required: AnyRef{def quack: Unit; def feathers: Unit}
       inTheForest("Duck")
                   ^

When is this useful? Mostly in situations where you are consuming classes that follow common conventions but have no shared interface, in this case you can define the parts you are interested in by using the structural type as an implicit interface.

Finally, if you find that you are using a structural type a lot, it can define it in one place:

object UsefulTypes {
  type Duck = { def quack; def feathers }
}

inTheForest can then be declared as:

import UsefulTypes.Duck
 
def inTheForest(duck: Duck) = {
  duck.quack
  duck.feathers
}

Written by mark

August 27th, 2008 at 6:40 pm

Project Cancellation

with one comment

A well run software project should have a set of objectives that it is trying to meet. Sometimes projects will fail or be cancelled when it becomes clear that those objectives cannot be met within constraints, such as time-scale or budget, that form part of the project’s business case.

When a project is cancelled, it can have a profound impact on the team. It is important to manage the process of cancellation so that team members can smoothly transition in to their next project.  How this is handled can impact team members’ productivity now and in the future, as well as their perception of the company and its managers.

Team members should understand the reasons for project cancellation and believe them, if possible the teams should be part of the discussions around cancellation even if they can’t influence them.  Once team members leave the project they should be rolled on to new projects as soon as possible and given productive work to do.

It is important to realise that project cancellation is often the right action.  Projects are planned based on imperfect information; as projects progress and teams learn more then the decisions on project viability can change too.  Often it is difficult to act on this changing information, particularly in public sector projects, as it could directly impact the reputation of the project sponsors, and undermine the strong emotional engagement that team members have made in delivery.  To make the best decisions, projects should be monitored continually, and early and regular feedback obtained from all project stakeholders so that informed decisions can be made and openly communicated.

At the end of a project, a project retrospective should be run to give the team an opportunity to take stock and take away lessons for the future.  Retrospectives should be balanced, looking at the good and the bad and providing a basis for team members’ future work and a celebration (cue the beer) of the teams achievements.

Written by mark

August 22nd, 2008 at 2:40 pm

Ever wondered where your time goes?

without comments

Slife is a great free app for the Mac that sits in the background and logs how much time you spend in each app that you have running.

By telling it when you start a new activity, it will give you an idea of how you spent your time during that activity.  So, for example if you are trying to ‘Implement a basic Rails app’, you can find out how much time you spent reading e-mail, browsing the web or editing your source.  I found it quite enlightening and it made me think more about ways that I can stay more focused.

If you want to understand where you time goes and you have a mac, Slife is the app for you.

Thanks to Floyd for the tip.

Written by mark

August 22nd, 2008 at 12:22 pm

Moving from Java to C#

with one comment

After about 10 years working primarily in Java, I’m a few weeks in to my first commercial C# project. The transition was much smoother than I’d expected and after some head-scratching around the syntax additions in C# 3, I feel productive.

I’m surprised how little time I have needed to spend learning the .NET framework libraries and exploring the .NET open source community.  As a business app, most of the code I am writing models the domain and interacts with other parts of the domain; it is all written by the team.  With the exception of things like the collections classes, most of the framework/library interaction is on the fringes of the system.  That said I’ve done my fair share of Googling things like ‘How do I parse XML in .NET’, but the community is large and so it’s easy to find solutions to problems. A harder part is learning C# idioms, the Manning book C# in Depth was great for this (see later) as were chats with my teammates and pair.

Along the way I’ve picked up a few useful resources:

Resources

Other thoughts

  • Get Resharper
  • If you are familiar with Java IDEs like Eclipse or IntelliJ and regularly re-factor then you will need Resharper. The out of the box support for refactoring is minimal even in VS2008.

  • Remember there is an eco-system outside of Microsoft
  • Many companies having selected .NET will automatically favour Microsoft solutions for everything. Even though the open-source community doesn’t appear as vibrant as for Java, it does exist. Check out alt.net too.

Written by mark

August 20th, 2008 at 3:45 pm

Posted in C#, Feed to Planet TW, Java, Software

Tagged with , , ,

Polyglot programming with IntelliJ 8

without comments

Artima interviewed JetBrains’s Dmitry Jemerov at JavaOne 2008 back in June, the interview notes are here: http://www.artima.com/lejava/articles/javaone_2008_dmitry_jemerov.html.

Dmitry makes some interesting comments around Polyglot Programming and how Jetbrains plan to add new language support to IntelliJ. I’m glad to see a move towards more modularisation and particularly a way of specifying which features to enable (e.g. Java, SVN etc) and even for disabling Java when using another language. This change should go a long way to addressing the bloat, and sluggishness, of earlier IntelliJ releases.

The most exciting feature for me is Scala support. I’m a big fan of Scala, but I see two barriers to adoption: the available documentation is limited, particularly good examples; and it’s quite a complex language. Dmitry touched on this last point but I don’t really agree that it’s as complex as C++. Artima have a Scala book in the works which is a fantastic way to pick up the language.

The IntelliJ EAP releases can be downloaded from http://www.jetbrains.com/idea/nextversion/.

Written by mark

August 19th, 2008 at 5:35 pm

Lotus Notes 8.5 won’t launch on Mac OS X 10.5.3/10.5.4

with one comment

IBM have released a public beta of Lotus Notes 8.5 for the Mac. It seems a lot better than version 7, but after applying the OS X 10.5.3 update I found that I could not start Notes. The only feedback of a problem was in the system log:

[0x0-0x34034].com.ibm.lotus.Notes[546] maclauncher: unrecognized option `-psn_0_213044′

The workaround I’m using is to run the ‘Notes’ application from inside the application bundle. From the terminal:

/Applications/Notes.app/Contents/MacOS/Notes

There is a discussion on the IBM forums covering this so I hope it will be fixed in the next public beta: http://www-10.lotus.com/ldd/nd85forum.nsf/DateAllThreadedWeb/d1ef5d3d0a322049852574600046e61e?OpenDocument

Written by mark

August 15th, 2008 at 4:17 pm

Posted in Feed to Planet TW

Radiohead in Victoria Park

with one comment

I saw Radiohead play in Victoria Park last night, great music, great beer, great company, great weather, long queue for chips ;-)  About half way through a Tibetan flag appeared on stage to chants of ‘Free Tibet’, I’m sure I heard a few shouts of ‘Free Beer’, honestly it wasn’t me!

Written by mark

June 25th, 2008 at 10:07 pm

Posted in Personal

LLVM-GCC and Clang

with one comment

At the WWDC, Apple talked about LLVM-GCC and Clang.  LLVM-GCC takes front end components of GCC such as the language parsers and builds LLVM platform independent intermediate language (similar to JVM bytecode).  LLVM can then provide link-time optimisations, JIT compilation, dynamic code generation for this IL.  Backends exist for emitting code for a number of processor architectures as well as MSIL.

Clang is an Apple led project to create a new C, C++, Objective C and Objective C++ front-end for the LLVM compiler, essentially removing the need for LLVM-GCC.  It’s an interesting move by Apple that leaves them free to better integrate a compiler into Xcode without some of the restrictions imposed by the GPL.  For a technical comparison with other compilers including GCC see here: http://clang.llvm.org/comparison.html

At first glance, LLVM is an interesting technology that seems to provide a more modular solution compared with compiling to JVM bytecodes or MISIL, allowing a language implementer to take advantage of the LLVM IL optimisations and runtime environment if they want to.  There is an LLVM sub-project called vmkit, which is working towards binary translation from Java bytecode and MSIL to LLVM IL, it’ll be interesting to see how it performs compared with Sun’s Hotspot VM which they have tuned over many years, some pre-Java.

Apple are targeting speed improvements and a reduced install footprint for Snow Leopard (OS X 10.6).  It seems that LLVM-GCC and maybe Clang are driving some of these improvements.  Apple are claiming around a 33% performance improvement when compiling code with LLVM-GCC compared with just GCC, and by shipping LLVM-IL applications rather than fat apps containing x86 and PPC versions they could reduce the install footprint.

This is very cool: Apple are using LLVM as part of the OpenGL stack in Leopard

Written by mark

June 25th, 2008 at 9:50 pm