Scala play anorm; Returned database object, get value of its properties in an unabsurd way
-
29-10-2019 - |
سؤال
Hello I am fairly new to Scala.
I want to learn Scala because:
- To get rid of boiler plate code from Java.
- Play with different paradigms.
- Just for fun of learning new things
I am following a tutorial on plays website.
Here is the tutorial code:
Player.create(Player(Id(1), "bob@gmail.com", "secret", "Bob", false))
Post.create(Post(NotAssigned, "My 1st post", "Hello world", new Date, 1))
To get deeper knowledge I played around and modified the examples.
I decided not to give the Player a predefined ID but let MySql assign it.
In MySql I have defined the Player id as an int but Post id as an long. // I will change this later but for this example it is important.
But what shocked me is the way I did it, look at the second row. This is not the elegant at all.
val player = Player.create(Player(NotAssigned, "bob@gmail.com", "secret", "Bob", false))
val id = player.get.id.get.get.asInstanceOf[Number].intValue()
Post.create(Post(NotAssigned, "My 1st post", "Hello world", new Date, id)
Now to my question how do I get the Id as an int from the created player in a less absurd way?
لا يوجد حل صحيح
لا تنتمي إلى StackOverflow