Best scala questions in September 2010

Creating Android apps without Java

11 votes

I'd like to start creating Android apps but I don't like Java. I read that scala can be used to do it. Are there another option?(Clojure?)

I'm a Python/Django developer so it would be great to learn a pretty different language.

At this point Scala is the one that is most mature..I wanted to try groovy myself but its not even out of alpha..

Plus Scala on android has docs..:)

What prevents a statically typed language from having something like Ruby's method_missing?

9 votes

I don't have much experience with statically typed languages (currently learning Scala and loving it!) but one thing I've noticed is that they don't ever seem to have anything like Ruby's method_missing or ColdFusion's onMissingMethod. Is there some inherent limitation in statically typed languages that prevent or make this difficult?

Certainly a mechanism for handling them could be added, but it is at odds with what static typing is: Compile-time determination that your program is free of type errors.

Scala: jaxb or similar?

7 votes

As a java programmer I'm quite comfortable with using JAXB and similar, for example to construct object from a XML spec.

I'm sure I can make JAXB work nice in scala, but I wonder if that is the scala way of doing it, or if there is some better/smarter way, especially since XML is almost part of the language / it's internal libraries.

So, given that I want to create a set of object form a XML spec, what's the recommended way to do this in scala?

For pure Scala, there is scalaxb. It generates case classes and parser based on Scala parser combinators. It's still quite young, but if you steer clear of the advanced features of XML Schema it is quite usable.

SOAP-proxy in Scala - what do I need?

5 votes

I'm trying to write a program in Scala, that will accept SOAP-requests, get the response from the real server (or read it from the local disc) and return the data to the original client.

I'm new to the java/scala ecosystem, so I have no clue, what libraries to choose. I heard Scala's XML-handling is quite nice, so I don't know, whether I should use some enterprisey soap-library/framework like jax-ws, jboss-ws, axis, cxf, xmlbeans, etc.

Basically, I just need

  • a library, that accepts the requests (currently, I'm looking at jetty, but I'd prefer something that natively supports actors. scala-http seems to cover that, but isn't production-ready or maintained, for that matter)
  • some library to request the data from the other server (something like curl, libwww-perl for java/scala)
  • a build system (ant? sbt?)
  • an IDE (I'm used to eclipse, but IntelliJ's scala support is supposed to be better)
  • a tool to test it (currently, I'm using SoapUI)

SOAP is truly a hideous specification, with lots of potential for unusual fringe behaviour. While it's true that XML support in Scala would help you write such a library from scratch, it would still be a major effort (depending on how much of the spec you need).

Likewise, Jetty has years of development behind it; dealing with performance demands and other unexpected behaviour that you probably haven't considered... Even Scala's best-known web framework, Lift, runs atop a Java web server for these very reasons. It still works very happily with actors.

So, at this moment in time, you're almost certainly better off using a tried and tested solution with a Java web server and an off-the-shelf Java SOAP Library. The effort to add a thin Scala wrapper around these will be far less than the effort to build these things from scratch.

For the build system, sbt is the most powerful tool presently available for Scala, but you may need to fall back to Maven if this is needed for code generation by your chosen SOAP library.

Finally, for the choice of editor. If you're happy using Emacs, then the Ensime plugin is just amazing. If a more conventional Java IDE is to your liking, then IntelliJ currently seems to be the most stable option, though be aware that this could change very quickly.

Accessing XML attributes with namespaces

5 votes

How one can access attributes with namespaces? My XML data are in a form

val d = <z:Attachment rdf:about="#item_1"></z:Attachment>

but the following does not match the attribute

(d \\ "Attachment" \ "@about").toString

If I remove the namespace component from the attribute's name then it works.

val d = <z:Attachment about="#item_1"></z:Attachment>
(d \\ "Attachment" \ "@about").toString

Any idea how to access attributes with namespaces in Scala?

The API documentation refers to the following syntax ns \ "@{uri}foo".

In your example there is no namespace defined, and it seems Scala considers your attribute as unprefixed. See d.attributes.getClass.

Now if you do this:

val d = <z:Attachment xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" rdf:about="#item_1"></z:Attachment>

Then:

scala> d \ "@{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
res21: scala.xml.NodeSeq = #item_1

scala> d.attributes.getClass
res22: java.lang.Class[_] = class scala.xml.PrefixedAttribute