How to pre-initialize objects in scala

Asked 2 years ago, Updated 2 years ago, 134 views

trite A

object B extensions A {
  println("B")
}

object Sequences A {
  println("C")
}

object Main {
  defmain(args:Array [String]):Unit={
    println ("RUN")
  }
}

At this time, the standard output is

RUN

will only be displayed.

Do this somehow without explicit access

B
C
RUN

I would like to see .I would like to initialize it before it is accessed in any way.
Is there a way to access and initialize the class object when you add a specific annotation, or to access the subclass when you specify type parameter A?

scala

2022-09-30 21:30

1 Answers

There's no comment, but if you use it, you'll get it right

BC RUN

You will see :

trite A

 object B extensions A {
   {
     println("B")
   }
 }

 object Extends A {
   println("C")
 }

 object Main {
   defmain(args:Array [String]):Unit={
     B
     C
     println ("RUN")
   }
 }

The Scala object is lazy, so I don't think there's any other way.

If you are not satisfied with that reply, I am sorry.I'm not Japanese, so I might have misunderstood the question.


2022-09-30 21:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.