8 ms·
Scala == Effective Java?
- thisisananth 16y agoDoes scala have that many third party libraries ala java ? Because these days for web application most of us use Java with Spring or apache commons.
- stonemetal 16y agoAnything you can use in Java is available in Scala.
- Stormbringer 16y agoWhat about annotations?
- wheaties 16y agoYou can use them too.
- fhars 16y agoAlthough stuff that relies heavily on reflection and runtime code generation tends to be tricky. Class files generated by the scala compiler may look different from class files generated by javac and contain additional data (while of course still being valid java bytecode), and some tools rely on accidental details of the javac code generator. Someone mentioned JPA in this discussion. But most things just work. Another issue is using scala code from java. As long as the code implements a plain java interface, everything is fine, but if you try to use something impemented with some of the features that java doesn't have, you will soon see some very stange mangled names. trö_%¿ is not a valid java fuction name, after all.
- Stormbringer 16y agoScala, like most/all languages that run bolted on top of the JVM can use almost all of the libraries that Java itself can use, since the Scala code gets auto-converted to Java classes. (Typically (in the examples I've seen) one Scala class gets converted into many Java classes) This is not entirely dissimilar from the way that JSP gets converted into a Servlet. Scala/AlternateLanguageRunningOnJVM fans please provide corrections as appropriate, but be gentle, I'm making some broad generalisations here with all the flaws implied by that methodology. :D
- beamso 16y agoThere maybe exceptions to the 'you can use all Java libraries' at runtime. I'm unable to use a Scala class annotated with JPA annotations with Eclipselink. The same class works fine with Hibernate.
- sigzero 16y agoI thought one of the premises of Scala was that you could use all of the Java libraries as well?
- masklinn 16y agoIt is, and you can.
- unoti 16y agoYes. Anything you can use from within Java, you can use from within Scala. I'm quite happily using JGroups and Netty from within Scala. The code actually looks a hell of a lot better than the equivalent Java versions.
- rit 16y agoAs many other replies pointed out, the answer is most definitely 'yes'. You can easily use any Java libraries from Scala and Scala provides a number of tools including JavaConversions and JavaConverters to automatically convert to and from Scala and Java collection objects for easy interop.
- Stormbringer 16y agoWhat is up with the dodgy numbering? I hope he didn't get a Scala program to do the list!!! I am always deeply suspicious of any appeal for using a language that uses as it's initial premise that the language is shorter or saves keystrokes. Personally, I'd take one of his examples and make it 'worserer' by enbiggening it. new RunningReporter(); public class RunningReporter extends Thread { public void run() { System.out.println("Running"); } } And then I'd redesign it to something useful, e.g. public class RunningReporter extends Thread { public void run() { System.out.println("Running"); doSomething(); } public void doSomething() {} } So then subclasses of running reporter would spit out Running to System.out.println and then go and do their funky thing. But even that isn't as useful as it could be, it should really be something like: public class RunningReporter extends Thread { String message = ""; public RunningReporter(String s) { super(); message = s; } public void run() { System.out.println(message); doSomething(); } public void doSomething() {} } Okay, so now you can customise the message, so that you can tell these threads apart if you're doing stuff: e.g. new BluetoothRunningReporter("Spp fix"); So we've passed through shorter, and gotten bigger, but the line that would be used in the program conveys enormously much more information. I find that upon examination, in a properly and well designed Java system almost all of the anonymity disappears... which makes me uncomfortable with that example of how Scala is better than Java, because in the real world, if you were doing it properly, you wouldn't do it that way in Java anyway.
- hamletdrc2 16y agoThe numbering is based on the "Tip" Numbers from the Effective Java book. Each tip is numbered and indexed, and it is common to use the tips during code reviews.
- msmith 16y agoI'm not sure if I'm following your point. Are you suggesting that anonymous inner classes aren't useful in a well-designed Java program? If so, I'd have to disagree. As an example, the Java Collections API (one of the most well-designed Java APIs, imho) uses Comparators extensively. It's pretty common to define the Comparator as an anonymous inner class when you need to do a one-off sort() or max() call. Scala just makes it easier to use this pattern. There's no need to go through the effort of wrapping the function in class if you're only going to use it once.
- kokoloko 16y agoI switched from Java to Scala and I'm not looking back.
- bubing 16y ago⌒╮WELCOME http://www.yessoso.com/ http://www.yessoso.com/ ----- ~ ¤ ╭⌒╮ ╭⌒╮ ╭⌒╭⌒╮╭⌒╮~╭⌒╮ HANDBAG 35$ ,)))),'')~~ ,''~) ╱◥█◣ ╱◥█◣ SHOES 35$ |田|田||田|田| CLOTH 15$ ╬╬╬╬╬╬╬╬╬╬╬╬╬╬ 2010 NEW Your fashion! Your season! Michael bag fashion tidalcurrent.the fashionable element "bag" you - glamour infinite. Mass commodity, low discount, the buy cheaper. { www. yessoso.com } mall price can buy eligible products and services. Select all kinds of women, men's shoes, etc, { www.yessoso.com } shopping navigation! In addition, the company has a good reputation, quality standard, At a reasonable price. In the past few years, have been deeply overseas My friends for their support. Therefore, please rest assured purchase. ====== Free transport====== !!!!!!!!!!!!!! (http://www.yessoso.com/ http://www.yessoso.com/) !!!!!!!!!!!! input this URL: (http://yessoso.com/ http://yessoso.com/) you can find many cheap and fashion stuff (jor dan s-h-o-e-s) (NBA NFL NHL MLB j-e-r-s-e-y) ( lv h-a-n-d-b-a-g) (cha nel w-a-l-l-e-t) (D&G s-u-n-g-l-a-s-s-e-s) (ed har dy j-a-c-k-e-t) (UG G b-o-o-t) WE ACCEPT PYAPAL PAYMENT YOU MUST NOT MISS IT!! ----------------------
- brown9-2 16y agoIsn't this a bit of an unfair comparison? Java the language is at least 8 years older than Scala the language. Scala had the benefit of learning from previous language's mistakes.
- thisisananth 16y agoBut it is evolution. isn't it and there be better software for all of us.
- masklinn 16y ago> Isn't this a bit of an unfair comparison? No, because it's not like Java was the first language to ponder about these, and it did not learn from previous languages (mistakes or successes). Pretty much all of Scala's improvements over Java predate Java itself by decades. Java simply refused to learn from previous language's mistakes. To some extent, C# did as well by starting out as a slightly improved Java instead of an actually good language, which means even with all the improvement they're doing since they're not deprecating and removing old crud the language is now chock-full of crap and far more complex than it could (and used to) be.
- bad_user 16y agoYou're a little unfair in regards to C#. Since C# 1.1 they've only added features that are mostly orthogonal to the existing ones. Secondly, the features they've added aren't half-baked. Sure there's room for improvement, but they behave as you expect them to, and the designers specifically leave room for improvement all the time. Sure, I hate that everything I want to do still has to be specified within a class / static method; that's Java-related legacy done probably because of marketing reasons, but C# has 2 things I wish for whenever I'm working with Scala ... - the ability to get the expression tree of a closure. There was a plugin for Scala at some point, but it's dead. - dynamic typing, which structural typing cannot really replace. You say chock-full of crap, but IMHO language designers should learn from its evolution, because it was a good one.
- masklinn 16y ago
- joshhart 16y agoMore about his Item 11 on cloning. Writing a case class gives you a "copy" method which is an abstraction of clone where you can write something like val newFoo = oldFoo.copy(bar = someBar)
- yahelc 16y agoShouldn't the headline here be Scala.compareTo(Java)?
- weavejester 16y agoOr: Scala compareTo Java ? :)
- aneth 16y agoI used Scala for some small projects. I absolutely loved much of it, although I found I got caught up in experimenting with syntax to learn "better" ways to the point of distraction. These days I use mostly ruby and have started on node.js. Ruby programmers don't seem to understand the importance of encapsulation and typing, which is quite frustrating and leads to much head banging. I yearn for the day Scala or something similar is broadly adopted. Interestingly, many of the best features of scala mentioned here are veey new to the language. Named parameters and built in copy for case classes were not available until a year ago. It's great to see a language implementing prudent improvements quickly - here's looking at you Java.
- samstokes 16y agoRuby programmers don't seem to understand the importance of encapsulation and typing Interesting juxtaposition. I wonder if it's a concrete benefit of static typing to make you more aware of encapsulation: that does resonate with my experience. Because functions and object methods have to have types that fit together, you're forced to think about what goes into and comes out of a given component, so you're thinking about component boundaries more often. I know when writing a Ruby method it's all too easy to just return the first intermediate result that makes sense, subconsciously thinking "let the caller process this result, and if it's messy I'll refactor it later". In Java, I have to write the return type and also return something, and if I planned to return an Iterable<String> but I'm tempted to return a List<Object>, the redundancy catches me out. I have to consciously make the decision: should I change the contract of this method by changing the return type, or should I instead massage the results into the form I planned to return? (I think the same holds for Scala, where I might not have to write the return type because it's inferred: if anything actually calls my method, the compiler will still remind me that the assumptions made at the call site don't line up with the assumptions made in the method. In Ruby, although I'll still have to change the caller if I change the return type, I'm more likely to do it mechanically, in a "I changed that so I have to change this" mindset, rather than stepping back a level and thinking "which one of these is actually right?".)
- aneth 16y ago
- strlen 16y agoIt's not that Scala is Effective Java, it's that "Effective Java" (the book) is a good set of patterns of software developments in many other object oriented languages. You'll note much of the same discussed in Design Patterns (unfortunately, in a more rigid and less accessible form) and visible in well written pieces of software in Java, C++ and many other languages. That's why I highly suggest reading Effective Java even if you hate Java the language / Java the culture, the latter symbolized by horridly designed and implemented frameworks like Spring which have turned design patterns into a set of buzzwords to liberally sprinkle.
- foamdino 16y agoI agree that the Java culture is significantly poisoned by the 'enterprise architect' culture, but Spring if anything has been the saviour of Java. Compare IoC (Spring setter injection), with J2EE+EJB2.x - it's night and day - and I know which I would prefer to code using. Spring introduced POJO programming and explicitly called out the crappy J2EE design patterns in favour of simplicity. Now, Spring has accumulated so much cruft (and become much more commercial with the vmware buyout), that it is becoming the new J2EE. I've been doing some android programming recently, and working with Java without IoC is simply unbearable.
- strlen 16y ago> Compare IoC (Spring setter injection), with J2EE+EJB2.x - it's night and day - and I know which I would prefer to code using. Spring introduced POJO programming and explicitly called out the crappy J2EE design patterns in favour of simplicity. Agree. J2EE is an even worse example of "Java the culture". For a much better alternative, see Guice. I personally prefer "it's okay to call new sometimes" Design Pattern to IoC containers (but occasionally I find myself using Guice).
- strlen 16y ago> I've been doing some android programming recently, and working with Java without IoC is simply unbearable. Can't you use Guice with Android? I'd be surprised if you can't.
- nimrody 16y agoAre there any plans to have the Scala compiler emit native code? An LLVM front-end would make it much more appealing for general use. Perhaps as a sane (?) alternative to C++ in the high performance computing arena.
- jonburs 16y agoYou'd need a pretty hefty support library to add the JVM-provided features Scala relies on. Garbage collection is obviously a major one, but there are also the pieces of the standard Java libraries that Scala builds on top of (though I'm not sure how deep those dependencies run). GCJ attempted to do this for straight Java; I don't believe many people consider it a success.
- krosaen 16y agorelated: Norvig's presentation on how many design patterns are unnecessary in dynamic languages http://norvig.com/design-patterns http://norvig.com/design-patterns
- guelo 16y agoI'm not familiar with Scala but I don't understand how it could eliminate the need for Item 14: Use public accessors methods rather than public fields. The point of using accessor methods is so that you can change the underlying implementation if necessary. If the getters and setters are compiled automatically for you it doesn't seem like you'll be able to change the underlying implementation without also changing the types of the methods.
- unoti 16y agoHere's how it works. When I say: class Kitty { val purrVolume: Int; } What Scala actually produces in Java bytecodes is something like this Java equivalent: public class Kitty { private int purrVolume; public void setPurrVolume(int purrVolume) { this.purrVolume = purrVolume; } public int getPurrVolume() { return purrVolume; } } Later, if you decide that you need to change it to do some special calculations in the getters and setters, you can add that to the Scala code. External users of the class will not need to be recompiled, because under the covers Scala was never actually exposing the public member variable.
- pretzel 16y agoI think you mean var, not val. If you were using val you would do: class Kitty(val purrVolue: Int) which would turn out to be in Java: public class Kitty { private final int purrVolume; public Kitty(final int purrVolume) { this.purrVolume = purrVolume; } public int getPurrVolume() { return purrVolume; } }
- narrator 16y agoOn the Java version you forgot equals() and hashcode(). Scala's case classes give you that for free.
- masklinn 16y ago> If the getters and setters are compiled automatically for you it doesn't seem like you'll be able to change the underlying implementation without also changing the types of the methods. You can trivially replace the auto-generated methods by manually crafted ones, keeping the same types (since they're a direct consequence of the original object's type: a String member will have an accessor of type `() => String` and one of type `String => ()`). If the compiler sees the "public" member already has accessors defined, it doesn't generate them. Could hardly be simpler.
- mgkimsal 16y agoIt looks like Groovy satisfies many of these checklist items too. Any reason beyond personal preference to go with Scala vs Groovy or JRuby?