Created on Jan. 13, 2013, 2:23 a.m. by Hevok & updated on Jan. 13, 2013, 2:29 a.m. by Hevok
**Scala** is a general purpose programming language designed (as a "better Java") to express common programming patterns in a concise, elegant, and type-safe way.
¶
¶
A class can be simply declared like this:
¶
¶
.. sourcecode:: scala
¶
¶
class ScalaUser(var name:String,var surname:String, var email:String){}
¶
¶
Getters and setters are easy to create:
¶
¶
.. sourcecode:: scala
¶
¶
val setters = tp.members.filter(m=>m.isMethod && m.asMethod.isSetter).map(m=>(cutSetterEnding(m.name.toString),m.asMethod)).toMap
¶
¶
Scala reads and writes as it would be normal English:
¶
¶
.. sourcecode:: scala
¶
¶
something should equal("sfsf")
¶
¶
It is a dynamically typed language and provides similar paradigms like Python. You can for instance simply iterate over a collection:
¶
¶
.. sourcecode:: scala
¶
¶
for{
¶
item<-collection
¶
if item=="somevalue"
¶
if item.isSomething
¶
} yield item
¶
¶
It is even possible to iterate over multiple collection at the same time:
¶
¶
.. sourcecode:: scala
¶
¶
for{item1<-collection1
¶
item2<-collection2
¶
if item1=="somevalue"
¶
if tiem2>item1
¶
} yield( item1.someProp,item2.someProperty)
¶
¶
A basic function/procedure looks like this:
¶
¶
.. sourcecode:: scala
¶
¶
def someFunction(param1, param2) =
¶
{
¶
///do something
¶
(val1,val2)
¶
}
¶
¶
It is not hard to work with OrientDB. To open a database:
¶
¶
.. sourcecode:: scala
¶
¶
val db:ODatabaseDocumentTx = new ODatabaseDocumentTx(url)
¶
¶
To create a document and fill it with data.
¶
¶
In order to save any class to the database all you need is two lines of code, considering you have created an instance of your class somehow:
¶
¶
.. sourcecode:: scala
¶
¶
val doc = new ObjectDocument(myPojo)
¶
doc.save()
¶
¶
Where myPojo is an instance of any class created before.
¶
It downes not work with nested classes yet, but works well with pojo's (classes with simple fields).
Comment on This Data Unit