How do I call Scala sealed traits from Kotlin? - scala

From what I've read, Scala sealed traits can be used for ENUM like behavior, but they are not as intuitive to use in Java or Kotlin.
Scala code (and Suzy MV)
package com.example
sealed trait Answer
case object Yes extends Answer
case object No extends Answer
case object Maybe extends Answer
Java code to call above Scala
import com.example.Yes$;
Yes$ myAnswer = Yes$.MODULE$;
How do you call this in Kotlin?

Backticks are your friends here!
Kotlin
import com.example.`Yes$`
`Yes$`.`MODULE$`

Related

Is hashCode of a scala enum is the same on different JVM (spark) ?

I read that a good practice for enum is scala is as follow.
I deliberately extends the class with Serializable for Spark.
sealed abstract class MyEnum(val nature: String) extends Serializable
case object A extends MyEnum("a")
case object B extends MyEnum("b")
The problem is that i want that the enum should be extensible by others, thus i have to remove the sealed keyword to enable this functionality
abstract class MyEnum(val nature: String) extends Serializable
case object A extends MyEnum("a")
case object B extends MyEnum("b")
On another file
import enumpackage.MyEnum
case object C extends MyEnum("c")
Knowing this issue with Java enum on Spark, i was wondering how is generate the hashCode from object that extends class that are sealed or not.
Is it safe to avoid sealed keyword for my purpose or do i have to keep it, why ?
If i have to keep it, is there any solution to my extensibility problem.
In programming languages, particularly in Java & Scala enumerations are not extendible by the user, but only by the owner of the enumeration and usually for good reason, control. If you are getting rid of sealed then don't consider it an enumeration, just consider it just like any other class with subclasses or subobjects.
As for your hashCode. You get that automatically with case class or case object, so there is nothing extra you really need to do.

Can we define trait inside a object in scala

I just started to learn scala and currently learning about Akka through this Learning Akka course
I'm confused about the code style, the author has created a trait inside a object.
object MusicController {
sealed trait ControllerMsg
case object Play extends ControllerMsg
case object Stop extends ControllerMsg
def props = Props[MusicController]
}
I understand that Scala object provides singleton ability and a way to define all static method in class through companion object.
Can anyone help me understanding this syntax ? Thanks
You will often see this with Actors. It is good practice to define the messages that an Actor responds to in its companion object, which happens here.
The sealed trait part isn't really necessary. You just often see this in Scala with case classes/objects. Also, being sealed means that you will get a warning if your match is not exhaustive when you pattern match on instances of it.

Detecting all classes extending trait

I have the following trait:
trait Tr
It's being contained in a module cdl-common.
I want to detect all classes/objects extending it during compilation of the module cdl-impl dependign on cdl-common. For instance:
class Cls extends Tr
In Java, I could use annotation processor and annotate these classes with some annotation and then analyze AST.
Is it possible to do so in Scala with macro without annotating the classes?
You can do this easily using ClassUtil .
For usage, you can refer the answer given here: Get all the classes that implments a trait in Scala using reflection

Instantiating all classes that extend trait in Scala

So I'm building a library, and the problem I have is as follows:
I have a trait, such as
package my.library
trait Animal {
def randomFunctions
}
What I need to know is all the classes the consumer code has, that extend/implement said trait, such as
package code.consumer
case class Cat extends Animal
case class Dog extends Animal
So in summary: inside my library (which has the trait) I need to find out all classes (in consumer code) that extend/implement the trait.
I finally solved this by using reflections (https://github.com/ronmamo/reflections) with the following little snippet:
val reflection = new Reflections()
reflection.getSubTypesOf(classOf[Animal])
An option would be to use a sealed trait. This forces all implementations of the trait to reside in the same file as the trait was defined.
This would break your separation of consumer and library code but you would be sure to get all implementations.
The only other option I can think of is to use an IDE, like IntelliJ which has an option to find all implementation based on given trait.

"dynamically" creating case classes with macros

I would like to create a macro generated hierarchy of sealed abstract and case classes. There was an example similar to this with http://docs.scala-lang.org/overviews/macros/typemacros.html but is is now obsolete. Is this still possible?
I think it would be incredibly powerful to generate a type safe AST for some specified grammar. Ideally with an IDE able to resolve all the classes.
First for some shameless self-promotion: Eugene Burmako and I are giving a talk on type providers, a closely related topic, at Scalar 2014 tomorrow, and I encourage you to take a look at the example project we put together for the talk if you're interested in this kind of thing.
While type macros are no longer supported, you can accomplish essentially the same thing with macro annotations from macro paradise (which is available as a plugin for Scala 2.10 and 2.11):
import scala.annotation.StaticAnnotation
import scala.language.experimental.macros
import scala.reflect.macros.Context
// Add constructor arguments here.
class expand extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro Expander.expand_impl
}
object Expander {
def expand_impl(c: Context)(annottees: c.Expr[Any]*) = {
import c.universe._
annottees.map(_.tree) match {
case List(q"trait $name") => c.Expr[Any](
// Add your own logic here, possibly using arguments on the annotation.
q"""
sealed trait $name
case class Foo(i: Int) extends $name
case class Bar(s: String) extends $name
case object Baz extends $name
"""
)
// Add validation and error handling here.
}
}
}
And then:
scala> #expand trait MyADT
defined trait MyADT
defined class Foo
defined class Bar
defined module Baz
You can add arguments to the annotation that will be available at compile time, allowing you to parse an external resource that you can use to generate the implementation of the ADT, for example.
Macro annotations are very experimental and their status is still up in the air—there's no guarantee that they'll ship with Scala 2.12, for example. Something similar (although not quite so clean) is possible using plain old def macros and structural types—see the example project linked above for more detail and some demonstrations. In any case, this kind of mechanism is of interest to many people, including the developers of Scala's macro system, so even if macro annotations disappear at some point down the road, there's likely to be some way to accomplish what you've described here.