cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Kossakowski <g...@tuffmail.com>
Subject Re: [C3] Sitemap implemented in Scala
Date Thu, 19 Mar 2009 00:54:04 GMT
Joerg Heinicke pisze:
> 
> I don't have the time to actually look into your implementation. I'm
> just wondering if all the insight views couldn't be used to improve the
> Java implementation rather than starting something in a different language.

It could.

What I'm doing at the moment is sort of wild experimentation that I don't know where will
lead me to. I'm reimplementing
sitemap in Scala because I thought that sitemap processing is something complicated enough
to get a real feel of
programming in Scala.

It's true that anything I write in Scala could be emulated in Java but when I think about
it I feel rather overwhelmed
by the possible verbosity of Java counterparts to Scala constructs. In my implementation I
exploit heavily strong typing
by defining lots of types and using lots of generics. I use closures and pattern matching
quite a lot.

Compare (Scala):
private def filterUnprefixedSimpleAttributes(attributes : MetaData) : Map[String, String]
=
      Map() ++ attributes.flatMap(attr => attr match {
          case ua : UnprefixedAttribute => List(ua.key -> ua.value.first.toString)
          case _ => Nil
       })

to translation of it (Java):
private Map<String, String> filterUnprefixedSimpleAttributes(MetaData attributes) =
{
     return (new Map<String, String>()).putAll(attributes.flatMap(
       new Function1<MetaData, List<Tuple2<String, String>>() {
        List<Tuple2<String, String>> apply(MetaData x) {
          if (x istanceof UnprefixedAttribute) {
             UnprefixedAttribute ua = (UnprefixedAttribute) x;
            return new List<Tuple2<String, String>>().add(
              new Tuple2<String, String>(ua.key(), ua.value().first().toString()));
          } else {
            return new List<Tuple2<String, String>>();
          }
       })
}


And no, I didn't take the most complicated example from my code because I don't it find funny
to write such a spaghetti
code.

So certainly I could write the same in Java but I honestly believe that in Java operating
on strongly typed, immutable
collections is NOT fun and that's the reason nobody seems to do.

And if you skim through my code you'll find that it's more or less only about mapping, reducing
and filtering strongly
typed collections.

Still example above does not prove anything general (like "Java sucks, let's do it in Scala")
but my reimplementation of
sitemap processing should provide a nice background for a discussion what we want to see in
C3.

For example, I'm interested in answering questions like if we are going to introduce any other
language apart from Java
in C3. Traditionally Cocoon had other languages integrated for years (Javascript comes to
mind) so it's rather natural
to ask this question. If answer was positive (based on some evaluation) another question that
pops up is why it can't be
Scala?

WARNING: Here follows some thoughts on personal experience with Scala that you my safely ignore
as they are rather
off-topic.

Might be that I'm extrapolating but my sitemap reimplementation in Scala did work just after
I finished all necessary
bits. I mean: I have written all the processing, integrated it into servlet so it could work
with the rest of C3 and it
did work just immediately. I didn't have any test-cases and I didn't run my code while writing
it.
After extensive testing with all samples C3 provides I found exactly one bug[1] in that code.
Of course, this does not
mean I don't have any other bugs but the probability that I have many of them is really small.
Does Scala make me magically not making any mistakes? Certainly not. It only informs me about
them at the time of
writing code by reporting compilation errors.

This experience convinced me to have following standpoint:
     If we are going to introduce any secondary language in Cocoon,
     I would really prefer if it was strongly typed one.


Oh, and if you wonder how on the hell I could be typing non-trivial stuff for more than one
day without introducing
almost any bugs. It might be because my code cotains 5 if statements, no return, break or
continue statements and only a
few raw loops (like "while",  "for" or "repeat"). I believe that's the reason...

And yes, I have to admit that sitemap processing is somehow special case where the power of
concepts I've used simply
shines.

[1] http://github.com/gkossakowski/apache-cocoon3/commit/845f7b61861827a17fb017625eb7f2281ac458df

--
Best regards,
Grzegorz Kossakowski

Mime
View raw message