score:2
How about DelayedInit
?
import scala.util.control.Breaks._
trait BreakInit extends DelayedInit {
def delayedInit(body: => Unit) {
breakable {body}
}
}
class MyClass extends BreakInit {
println("Hello World")
break()
println("Error - should not be reachable")
}
val a = new MyClass
In this case, we've created a trait BreakInit
that inherits from DelayedInit
, which overrides the delayedInit
method to call the constructor within a breakable
block. MyClass
's constructor is then rolled up into a function, and passed to the delayedInit
method. As @som-snytt points out, the DelayedInit
magic only affects class constructors, not trait constructors.
However, I'd advise a note of caution when using DelayedInit
. It involves some reasonably deep compiler magic, and magic always comes with a price. Personally, I'd tend to prefer your original approach, as there's less magic involved.
Source: stackoverflow.com
Related Query
- DRY up boilerplate in object/class default constructor
- Instantiate a Scala class from Java, and use the default parameters of the constructor
- Scala Jackson object mapper setting null instead of default values in case class
- A class imported from a companion not usable as the constructor parameter default value
- Scala: default value in case class constructor doesn't work
- Scala class constructor default arguments with expression using previous members
- Import implicits from object that passed as method parameter vs class constructor parameter
- Instantiate type-parametrized class at runtime with default constructor
- Taking in any parameterized object as an argument to a class constructor
- Extends class with constructor and companion object
- Scala - How to create a class with a constructor that receives an object as parameter
- Scala:No suitable constructor found for type [simple type, class Thing]: can not instantiate from JSON object
- Default Constructor of Class
- Overwriting the default case class constructor (Scala)
- Case object extending class with constructor in Scala
- Putting object of scala.collection.iterator to the scala class constructor
- Scala - Why can't a context bound case class be used as a default constructor parameter of another case class?
- How to call a default function on instantiating a class object
- How should I pass a computed object from the primary class constructor to the supertype constructor?
- Difference between object and class in Scala
- Do scala constructor parameters default to private val?
- scala class constructor parameters
- Using .tupled method when companion object is in class
- How to check constructor arguments and throw an exception or make an assertion in a default constructor in Scala?
- Scala case class private constructor but public apply method
- Get companion object of class by given generic type Scala
- error: expected class or object definition
- Running a method after the constructor of any derived class
- How to provide default value for implicit parameters at class level
- How do I declare a constructor for an 'object' class type in Scala? I.e., a one time operation for the singleton
More Query from same tag
- GET form in Scala templates
- PlayJson update on element of array does not work
- How can I prevent Scala from evaluating arguments before they are passed to the macro implementation?
- Spark Hadoop Failed to get broadcast
- Scala case class with escaped field name throws error during Spark Catalyst code generation
- costly computation occuring in both isDefined and Apply of a PartialFunction
- Overriding Java method in Scala using eclipse
- scoobi concatenate DLists issue
- scala call tuple element with variable
- Play framework 2.7.2 migration, BodyPrasers.parse NoClassDefFoundError
- Optimize Spark job that has to calculate each to each entry similarity and output top N similar items for each
- Implementing Receiver in SPARK
- How to get specific fields in the List
- Difference between two rows in Spark Java API
- How to check if there intersection in list of pairs?
- On the performance of copying case classes
- What is the difference between using the return statement and defaulting to return the last value?
- How to extract a specific text from the string variable in scala
- Why is A => String not the same as _1.type => String where val _1: A?
- Not able to declare String type accumulator
- Drawing a SVG with d3.js while tab is in background
- Implementing Java interface in Scala results in incompatible type map
- How to implement that generic function with TypeTag in Scala?
- Can't pass environment variables to an sbt task in intellij
- akka-http send continuous chunked http response (stream)
- How to reference third-party libraries, eg breeze, from eclipse, when working with Scala
- Extracting Object from Some() and Using it
- upate a column based on multiple columns
- Pattern matching generic case classes for compiler
- What do _._1 and _++_ mean in Scala (two separate operations)?