Thursday, October 13, 2011

OMG, scala is a complex language!

NOTE that I cross-posted this to http://blog.coolscala.com/2011/10/omg-scala-is-complex-language.html


I keep seeing this and, maybe it’s true. Let’s chase this complexity for a bit and go through some of the biggest scala differentiators (from Java or C++, as major OO languages).
Smart compiler infers types
The compiler can infer the types for the most part, so people have to type a lot less repetitive information, which they used to type in both Java and C++. For instance, since “john” is obviously a String, the type of the variable is inferred by the compiler to be String so I don’t have to type it again:
val someone = “John”
I have seen this feature both praised and held against the language as “added complexity”, so I don’t know what to say. I just love typing less and feeling less stupid, every time I declare a value or variable.
Simplified class definitions
Since OO is all about defining classes, scala made do with a bunch of stuff in one go, so that my domain models are dead-simple:
class Person (val firstName:String, val lastName:String)
This, in Java and C++ takes about one page of code: with constructors, getters, setters etc. Scala observed that people don’t need to type one page to inform a stupid compiler that they want a person with a first and last names, so it’s all condensed in this one line, much like a table would look in SQL.
Is this added complexity? Well, I do need to worry about overriding the generated getters/setters ONLY if I need to, so I don’t really know if it’s more complex.
Me? I love this particular feature so much, that honestly, I don’t care what you think J
Unifying methods and operators
All programming languages I know discriminate between methods with names like “append” and operators like “+=”. Some do not even allow re-definition of some hardcoded operators (Java) while some allow infix notation only for operators (C++).
Scala simply makes do with ALL these restrictions and states that the name of a method can be pretty much anything and all can use the infix notation, so I can have:
ListBuffer(1,2,3) append 4
As well as
ListBuffer(1,2,3) += 4
The only difference would be the precedence rules, which are customary in all languages.
Some people obviously would see this as “more complex” than both Java and C++ since they can now do whatever they can… but I see it as “simpler” than both. Operators have been held against C++ before so it really is not surprising that they are held against scala as well.
This is, after all, what makes scala such as perfect DSL framework, allowing natural language such as:
“Mary” with “a purse” and “red shoes” should look “nice”
Types – variance
In both Java and C++, the generics have certain hard-coded and limited behavior (i.e. non-variance) and allow only a few constructs (like List<T extends Person>).
In scala, there is a default behavior, where List[Person] is non-variant, but everything is customizable. If you want co-variance, just tell the compiler List[+Person] or List[-Person] for contra-variance. Just like Java, I can use List[T <: Person] but I can use the reverse just as well: List [T >: Person].
Since scala supports implicits (with finer control than C++), another construct is available: List[T <% Person].
Is this more complicated? Well, just like the operators – it lifts certain limitations of other languages, so it’s both more complicated, since there’s more stuff to learn and simpler, since there’s less rules to live by.
I personally enjoy the extra control… do I actually use it? Not on a daily basis, the defaults are good enough.
Constructors only?
Most languages only allow data types (objects) to be constructed. This is normal in Java and C++.
Well, there’s the flipside, where I can de-construct an object and I don’t mean de-allocating its memory. Consider this:
someone match {

  case Person(first,last) => println (“ name is “ + first + ” “ + last)

}
You can see what I mean by de-constructing: took an already created object, someone, and de-constructed into its components. I know this looks foreign to most OO personnel, but trust you me, it is insanely cool and useful. Think what you would have to type in either Java or C++ to achieve the same thing, with if (instanceof) and then type cast and assign two variables and whatnot.
Is this more complex? Well, this is totally new functionality so I guess it is. But I love having it! Trust me, you will, too!
By the way, the match/case construct is way more powerful than your regular switch/case which can only handle constants… we can de-construct types, match constants, match types… and more! Check this out:
someone match {

  case Person(first,last) => println (“ name is “ + first + ” “ + last)

  case “John” |”Mary” => println (“hello, “ + someone)

  case s:String => println (“name is “ + s)

  case _ => println (“don’t know what this is…”)

}
Is this more complex? I don’t know… in Java or C++ this is between one and two pages of code. This looks simpler and more intuitive to me… granted, I got used to it but so can you!
Conclusion
There’s more areas of the language, but these are some of the major differences I have time for right now. If you have others, post up and I’ll get into those as well.
I did not get into the functional areas of the language, since that would require comparing with other functional languages and I’m not an FP guy. C++ comes close by allowing passing pointers to methods to other functions while Java 8 I think has some proposed lambda syntax.
Is it more complex? Well, there’s two ways to look at it:
Yes,
  • There are more symbols and features that one can use
  • There’s more computer science I need to learn (contra-variance, pattern matching, lambdas, closures etc)
No,
  • The real-world problems to solve are the same and
  • To do the same in either Java or C++ is either impossible or takes many times more code… and uglier code at that
What do I think? I don’t really care. To me it was cool to learn these concepts that I had forgotten since university and my new vocabulary allows me to solve the usual problems in just a few lines of code and head for an early lunch, while my mates are still writing some getter or setter…
What do you think?
P.S. This is a great detaiked discussion of complex vs complicated: http://lamp.epfl.ch/~odersky/blogs/isscalacomplex.html



Wednesday, October 12, 2011

Quick scala – step 1: setup

This post has a new home. If you want to setup a scala development environment, read: http://blog.coolscala.com/2011/10/quick-scala-step-1-setup.html

Tuesday, April 05, 2011

My worst internet shopping experience - gogglesgiant.com

My worst internet shopping experience, by far, has been with GogglesGiant.com , trying to get replacement goggles for my son's ski goggles, in the middle of the season.Order two pairs, in early February, from their online store at gogglesgiant.com, promises of fast shipping etc.

After two weeks I ask them that I haven't' seen a shipping notice and what's the delay and they reply that there's a high order volume and will ship when they can! Seriously, I will forward anyone their email!

I know I should've asked for the order to be cancelled right there, but I can't take a hint, eh?

After another week I receive a shipment notice with no tracking number. After another 3 weeks I receive the package, for a grand total of 6 weeks.

By now is late March and the season is basically over!

Done? Nope.They screw up and send me the wrong size for one of the lens. I write them back with a photo of the SKU and get ignored! After 1 week, I write again and receive this reply: mail us back the wrong lens and we will refund the cost of the lens. They refuse to send me the right lens, will just refund the cost, without shipping charges.

Reason: "we can't modify the original order". Seriously? I know it sounds too hard to believe, but these guys are for real! Again, I will forward anyone their original emails!

I get stuff shipped for free from Hong Kong in 3 weeks and these guys take 5-6 weeks! They refuse to make good on the order! It's insane, really! I would seriously suggest you look elsewhere for goggles. Ebay hasn't failed me yet. Actually this was the first time I strayed from Amazon and Ebay and I get screwed.

Also, they are a Yahoo Store and theirs is the worse complaining and merchant rating system ever. I hope they go banckrupt. I will and you should avoid any so called Yahoo Store.

I won't send them the wrong size only to get screwed again. I'll loose 40$ but I won't throw more good moneys after the bad.

Again - Goggles Giant cost me 40$, send me the wrong size, didn't send the order until after the season was over and have the worse customer service online... who lies through their nose!

Unfortunately I don't have a lot of recourse here. I can't even open a ticket with my credit card company because it has been like 9 weeks already now... smart buggers! Yahoo stores accept complaints only 6 weeks and after 30 minutes of browsing their website I can't even figure out how to rate this store or open a complaint!

Argh!

Friday, January 21, 2011

Scala DSL - realistic looking options

Trying to simulate the way options are passed to unix commands via '-'.

This post was moved to http://www.coolscala.com/wiki/Cool_Scala/Scala_DSL_-_realistic_looking_options