Skip to main content

All Questions

2 votes
1 answer
51 views

Scala initialization order object vs. val

In the following program an object is reported as null, even though it is clearly initialized. class MyClass() class MyOtherClass(c: MyClass) object Foo { val myClass = new MyClass() object ...
bobismijnnaam's user avatar
0 votes
1 answer
44 views

Instantiate lazy val which depends on runtime parameter in concurrent env

I need to instantiate MyHttpClient as Singleton. MyHttpClient is used by multiple threads. Solution below works, but it's ugly and should have issue with endpoint, I just didn't hit it yet. endpoint ...
Capacytron's user avatar
  • 3,758
1 vote
1 answer
304 views

Scala proper way to initialize ones changed in runtime fields: placeholder/null, None or zero element?

I got class with fields which value at initialization is unknown. But after, in runtime that values is will gained and setted to fields just ones. I want to decide what first initialization is best ...
Gudsaf's user avatar
  • 337
2 votes
0 answers
223 views

lazy initialization for Scala?

I have a Scalatest with the following structure which is emasculated within a class var dataHolder: Holder // some lazy initialization here? def runTest(filePath: String): Unit = { //Test 1 ...
SLN's user avatar
  • 5,112
3 votes
1 answer
346 views

Wordspec "should" "when" "in"

I'm initializing a companion object for one of my scala test suites. One of the fields in this companion object is lazy evaluated and uses some of the fields in the test suite for initialization. ...
Jackson Kelley's user avatar
3 votes
1 answer
361 views

How can I do pattern matching on tuple with lazy initialization?

I have an scenario where I need to call up to three services to do something. Each service has some kind of priority and my algorithm depends on the combination of the result of each service (all of ...
Sebastian Celestino's user avatar
2 votes
3 answers
2k views

Is it possible to declare a val before assignment/initialization in Scala?

In general, can you declare a val in scala before assigning it a value? If not, why not? An example where this might be useful (in my case at least) is that I want to declare a val which will be ...
Michael Blankenship's user avatar
0 votes
2 answers
258 views

How to Initialize an Immutable val outside scope of class

I was wondering if there is a way in scala to define an immutable value without initializing it to any parameters until it is explicitly called with a value to initialize it outside the scope of that ...
Almanz's user avatar
  • 89
2 votes
2 answers
122 views

Strange behavior of Scala compiler when initializing a class with a lazy argument

How possible that the first is correct Scala code but the second won't even compile? The one that does compile object First { class ABC(body: => Unit) { val a = 1 val b = 2 println(...
tartakynov's user avatar
  • 2,858
0 votes
1 answer
560 views

scala using a class argument a placeholder (or input variable)

Lets say i have a data structure that holds a parameterised type of data: case class Terminal[A](value: A, name: String ="") I can easily create a Terminal[Double] if i pass it a materialised ...
raul ferreira's user avatar
30 votes
2 answers
20k views

Difference when serializing a lazy val with or without @transient

Working on spark, sometimes I need to send a non-serializable object in each task. A common pattern is @transient lazy val, e.g class A(val a: Int) def compute(rdd: RDD[Int]) = { // lazy val ...
Hao Ren's user avatar
  • 986
2 votes
1 answer
1k views

Can Try be lazy or eager in Scala?

AFAIK, Iterator.map is lazy while Vector.map is eager, basically because they are different types of monads. I would like to know if there is any chance of having a EagerTry and LazyTry that behave ...
Trylks's user avatar
  • 1,498
2 votes
2 answers
148 views

How to find unreferenced objects in scala

I'm trying to create a map of all objects extending a specific class. I want to have access to this list before the objects are used in any other way. I tried this code: import scala.collection....
gens's user avatar
  • 1,012
0 votes
1 answer
231 views

How to avoid lazy vals when using cross dependent traits in cake pattern without additional traits

Suppose we have scala> trait A { val y: Int; val x = 1; val y2 = y + 1 } scala> trait B { val y: Int = 1; val x: Int; val x2 = x + 1 } scala> class C extends A with B Then both y2 and x2 ...
samthebest's user avatar
  • 31.6k
1 vote
0 answers
312 views

usage of `def` vs `lazy val` vs `val` for configuration properties in Scala

I have an application which is capable of reloading the properties whenever they are changed while the application is running i.e., on the fly! I created a Singleton to hold all the configuration ...
Sudheer Aedama's user avatar

15 30 50 per page