开发者

How can I use the Google App Engine Datastore from a Scala app?

开发者 https://www.devze.com 2023-03-04 09:40 出处:网络
I\'m writing a Scala app, to be deployed on the Google App Engine. I\'ve tried following the instructions here for using entities in the datastore, but the code won\'t compile.

I'm writing a Scala app, to be deployed on the Google App Engine. I've tried following the instructions here for using entities in the datastore, but the code won't compile.

import java.util.Date; 
import com.google.appengine.api.datastore.DatastoreService; 
import com.google.appengine.api.datastore.DatastoreServiceFactory; 
import com.google.appengine.api.datastore.Entity; 
// ... 
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 

This gives me an error on the last line, which says:

"Expected class or object definition" 

Is there diffe开发者_JAVA技巧rent code I should be using because I'm writing in Scala?


Yes, there is different code that you should be writing in Scala.

For example, to create an integer object with the value 42 you would write:

var theNum = 42

or perhaps

val theNum = 42

Note that your long statement does not include either var or val.

Also, you can leave off a lot of the stuff that you write in Java. I expect that it will work if your simply write:

val datastore = DatastoreServiceFactory.getDatastoreService

The Scala compiler will infer that it is a DataStoreService. Just a waste of time to write that except in function/method definitions. The compiler will also infer the semicolons at the end of most lines. I'm not sure about whether or not the compiler will infer () in the method call but why not try it and see if it does. This is a new language and you need to get used to the idea of writing things differently.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号