Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts

Tuesday, 25 August 2009

Learning Scala when Sleep Deprived

So I've not blogged anything in a while, usual excuse of being busy, the not so usual excuse of becoming a Dad for the second time. So as well as being busy I am somewhat sleep deprived.
I've been doing some stuff with Scala and Lift, and hit a really basic issue that took me a moment (in my current state that equals about an hour) to understand.
I had a couple of classes and was trying to access a field in one class from another ... well I thought it was a field.

package foo

class A( name: String ) { }

class B() {
  val myA = new A("foobar")
  println( myA.name )
}

When I compiled I got this error:

error: value name is not a member of foo.A

Huh? Fields are public by default in Scala, what is going on here. It really had me stumped, my addled brain failed to find any reason for this, and a google search found nothing really useful.

So I changed my code to...

class A( name: String ) {
  def getName = { name }
}

class B() {
  val myA = new A("foobar")
  println( myA.getName )
}

Now I was calling a method which simply returned name, this compiled and ran fine. So why did my original code not work? And why if I first thought that perhaps constructor parameter generated fields were private, but why did I not get an accessibility error?

Then I had a thought, how does the compiler know if 'name' is a val or a var? What if I explicitly state so?

class A( val name: String ) { }

class B() {
  val myA = new A("foobar")
  println( myA.name )
}

This worked. Interestingly using 'var' worked as well - I can only assume that the compiler is getting confused if its not specifying it and so silently doesn't create a field. Even though you can still reference it internally in the class it has no external representation.

Perhaps a case for either a warning or better error granularity?

Or perhaps just a case for me to get more sleep?

Tuesday, 14 April 2009

Why do we assume they know what they are doing?

I had a interesting thought whilst talking to someone about the whole 'Twitter using Scala' thing.

Now I have no vested interest in either Ruby or Scala. I like both languages, I tend to lean toward Scala as I am a staticly typed kind of guy, but in the end I like to think I am a pragmatist and will use the right tool for the job.

But what got me after looking at all the blog posts, all the tweets and email threads was, why do we assume that they (Twitter) know what they are really doing? Now I am by no means having a go a Twitter, I think it was a brilliant idea, it has captured the imagination of many and I see plenty of people that bag it still updating their status every hour or more.

But coming up with a great idea for a service doesn't mean you instantly know the right way to implement it - especially if its never been done before.

So Twitter, its kind of like a global IRC service, its web-scale. Did the guys that came up with the idea have any experience with building web-scale services?

(Hint: buy Jim Webber a few beers if you want to know how to play nicely at web scales and how to leverage its nature to your advantage).

So as the debate over Ruby-vs-Scala continues, which I think is irrelevant, the real questions on the learning Twitter has gone through linger on. Have they learnt enough about the domain (web scale messaging)? Has the underlying Architecture improved or does it remain the same regardless of the language of implementation?

My guess (and it is a guess) is the Architecture remains the same. In that case they may have undertaken what has been done many times before and just reimplemented it but in another language. They may have even done it right, managed to get a clean break from some technical debt that was dragging them under, but in my experience, that never quite happens either. So you are still faced with technical debt (possibly even more) as well as a sub-optimal architecture.

Having said all of that, it is worth keeping in mind that Twitter is still here today and is still increasing in popularity despite the "Fail Whales". And I will continue to use it.

And in reality this doesn't just go for Twitter, theirs just happens to be more public than most.

So a reminder (to myself as much as anything) - Before reaching out for that implementation solution, that golden hammer - its worth stepping back, taking a break and looking at the architecture and even the requirements and asking is that really valid, does it really have to be 'that' way? Are we making life more difficult for ourselves.