Tuesday, June 08, 2010

Where scala left me wanting

n the past few years, I've grown to enjoy more and more programming in a functional style. Even the java code I write nowadays is mostly free of mutable state. But before that, I spent a good chunk of time learning about object orientation and found some great ideas there. Ideas that can carry over to the functional world. One of them is Domain Driven Design, where we strive to have the symbols in the code respresent conncepts in the domain. Of course, I'm oversimplifying DDD, but the ideal of a bijection between the reality our software models and constructs in a programming language is one I believe we should maintain in many, perhaps most, applications.

A natural consequence is that our systems architecture tends to resemble an onion, with the domain model in the center and code to make it interact with the rest of the world surrounding it:

I've labeled the code that interacts with the rest of the world "Adapters", as it should do little more than adapt external representations into concepts the domain model can understand. This architecture is not in any way novel. In fact, it is a version of Alistair Cockburn Hexagonal Architecture. In the common case where the only interactions the domain model has with the rest of the world are through a Database on one end and an User Interface on the other, we've just described the 3-layer architecture that was so popular in the 90s. Some examples of adapters are — to increase our shot at the buzzword bingo — Web MVC Controllers, Repositories or DAOs, message endpoints, GUI listeners, etc., you get the picture.

Since Scala is a truly hybrid language, marrying quite elegantly OO and FP, it would appear to be the perfect vehicle to write this kind of software. I've found this premise to be correct, for the most part. As you might have guessed, there is an exception; (If there weren't, I wouldn't be writing this post, would I?). The problem is in the code for the adapters. As mentioned, they tend to be simple shims, translating some external representation into domain objects. Coding such translations every single time for every adapter instance in every application is very dull. We desperately need our old friend, lady abstraction, to help us out here. In the Java world, she lends us a hand though dynamic reflection, allowing the construction of objects and invocation of methods to be done generically. Unfortunately, Scala has no reflection API, so we have no alternative but to resort to Java reflection, in a sense reverse-engineering the Scala compilation process.

This isn't a major gripe, as Scala constructs tend to map to Java constructs in a straightforward fashion. Also, rumor has it that a Scala reflection API is being developed for 2.8.1. But that's only half the story. Powerful as it is, dynamic reflection alone is not enough to solve the Adapter problem once and for all. We often need to parameterize the translation in some way. For instance, when translating objects to database rows, we must discover the Column names corresponding to object properties. In many cases we can trust convention, for instance we could expect the columns to match exactly the names of the properties. But in other cases there is no option but to somehow configure the translation with an explicit mapping.

 In the Java world this kind of thing used to be done with verbose and annoying XML configuration files. As the language evolved, annotations were introduced, and they are now the main way to configure such translations.

Annotations are a great improvement over external XML files, but they can't be the end of the story. There is the relatively minor issue that annotations pollute the domain model we strive so much to keep clean and organized, specially when the same domain objects will be active in many adapters. Beyond that, there is the larger problem that annotations are just metadata. Sometimes we want to parameterize our adapters in richer ways. Take, for instance, the proposal for a typesafe API for database queries to be added to the new version of the Java Persistence API. It requires a special pre-processor to generate a metamodel that can be used to parameterize a database adapter. 

A better and more generic approach would be to have the language itself provide this metamodel: a kind of static reflection. I would love to see something like this in Scala. Some time ago I toyed with the idea of writing a compiler plugin to provide a static meta-model of Scala classes, but apparently compiler plugins have issues with non-transparent code generation. 

Postscript. As with all matters regarding programming languages on the web, we must tread lightly. I am not ranting against Scala, in fact I rather enjoy it, as can be gleaned from some of the previous posts. I am only relating a very specific domain where I believe the language can be much improved. 


Alex Cruise said...

Your images seem to be broken links to Google Docs that I don't have access to...

Dave Orme said...

Two words: Eclipse EMF

It's the best way I've seen to code a domain model.

Unknown said...

@Alex Cruise.
Sorry for the mishap, the images should be correct now (thought a tad too ginormous, I'll correct this later)

Rodrigo Kumpera said...

This could be done trivially if all constructs of the language where allowed as generic arguments.

Well, actually all you need are dependent types and functors since all other elements can be encoded with those two.

Anonymous said...

Could some of the things you are looking for be resolved using the undocumented scala.reflect.Code feature?


Unknown said...

@Rodrigo Kumpera -- But what would be the values inhabiting those types? Maybe something like the (IMO) hackish Manifest mechanism?

And the frontier between "normal" type-systems and dependent type systems is not an easy one to cross.

Unknown said...

@Anonymous Thanks for the pointer. Expression trees are certainly one way to do static reflection, and they could probably be exploited to do everything I outlined. But I still do like the idea of having an explicit metamodel.

Priscilla said...

Ãh? Arquitetura da cebola? Muito aplicados os temas daqui, passei pra olhar só de curiosa. Gostei das citações do post anterior. :) Bjo

Anonymous said...

AFAIK there is a reflection API, now. I'm not sure if it is still experimental but there is something to start with.

Anonymous said...

air bangs long hair extensions curly hair, hairdressers said thathair extensions sale this hair is relatively wild, and the operation is relatively simple, the hair extensions uk color can also have a variety of options, for a higher number of lush hair extensions girls, this kind of wig sets perfect.

Unknown said...

Nice post! Acadecraft has a team of experts that have the experience and knowledge to answer intrinsic questions in a detailed and step-wise manner. Our team of manual designers answers every question accurately.
manual development services
video solutions development companies
higher education services provider

opbest said...

Fantastic website. A lot of helpful info here. I am sending it to some
friends ans additionally sharing in delicious. And obviously, thank you to your effort!

my web page; 대구오피


JACK Charles said...

I absolutely agree with your observations about Scala and the difficulties it brought. It's encouraging to hear someone express what it was about the experience that left them wanting more. Your observations highlight the significance of linguistic inclusivity in the field of programming. It's essential to comprehend explain how inclusive practice promotes equality and supports diversity in order to make the workplace more approachable and friendly for developers from all backgrounds.