companion-object

Scala: methods in trait can't use methods from companion object

两盒软妹~` 提交于 2020-07-02 03:10:52
问题 When I try to compile the following I get: "not found: value cons" and "not found: value empty" for the take and drop method definitions. Somehow the trait doesn't "see" the companion object? I'm using IntelliJ IDEA, in case that matters. import scala.annotation.tailrec object Run extends App { sealed trait StreamRed[+A] { def headOption: Option[A] = this match { case Empty => None case Cons(h,t) => Some(h()) } def toList: List[A] = { @tailrec def toListRec(stream: StreamRed[A], accumulated:

Access application context in companion object in kotlin

笑着哭i 提交于 2020-02-27 22:29:09
问题 How can we access application context inside companion object in Android kotlin? I have a companion object inside an abstract class and I want to access context to read Shared Preferences, but I'm not able to get the context. UPDATE: I'm working with this stuff in an Android library and also the class that I'm working in is abstract 回答1: please see this go to link class MainApplication : Application() { init { instance = this } companion object { private var instance: MainApplication? = null

Generate companion object for case class with methods (field = method)

二次信任 提交于 2020-01-15 11:46:06
问题 Generate companion object for case class with scala-macros some code example that i tried, it works i can get list of tuple (name -> type) but how to generate the object in the same scope? import c.universe._ val tpe = weakTypeOf[T] val fields = tpe.decls.collectFirst { case m: MethodSymbol if m.isPrimaryConstructor => m } .get .paramLists .head val extractParams = fields.map { field => val name = field.asTerm.name val fieldName = name.decodedName.toString val NullaryMethodType(fieldType) =

Android/Kotlin: Error: “Expecting a top level declaration > Task :app:buildInfoGeneratorDebug”

百般思念 提交于 2020-01-05 05:45:07
问题 I try to write a class to manage a SQLite DB, but I have the error message "Expecting a top level declaration > Task :app:buildInfoGeneratorDebug". package com.xexxxwxxxxs.GMP import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.content.Context import android.content.ContentValues class DBHandler(context: Context, name: String?, factory: SQLiteDatabase.CursorFactory?, version: Int) : SQLiteOpenHelper(context, DATABASE_NAME, factory,

When to use companion object factory versus the new keyword

本小妞迷上赌 提交于 2020-01-01 08:42:28
问题 Many classes in the Scala standard library use apply() of their companion object as factory. This is often convenient when chaining calls like List(List(1)) . On the other hand, it's still possible to create objects directly with new ( new HashMap[Int, Int]() ). That's standard library. Now, in my own code, which approach is better to use: companion factory or creating objects with new ? Is there any convention on when to create companion object factory and when to do with the new keyword?

calling methods of Controller from scala companion objects

淺唱寂寞╮ 提交于 2019-12-25 07:15:12
问题 I have a controller in my project which has a socket method I want to call that method in companion object.But somehow i am not able to do that as i need to pass parameters also to companion object , which i can't . Here's my code sample: class WebSocketController @Inject() (cache:CacheApi)(implicit actorSystem:ActorSystem, materializer:Materializer) extends Controller { def socket = WebSocket.accept[JsValue , JsValue] { request => ActorFlow.actorRef(out => SocketHandlerClass.props(out

Kotlin - accessing companion object members in derived types [duplicate]

淺唱寂寞╮ 提交于 2019-12-24 12:20:00
问题 This question already has answers here : Kotlin: How can I create a “static” inheritable function? (3 answers) Closed 2 years ago . Given the following code: open class Foo { companion object { fun fez() {} } } class Bar : Foo() { companion object { fun baz() { fez() } } } baz() can call fez() I can call Foo.fez() I can call Bar.baz() But, I cannot call Bar.fez() How do I achieve the final behaviour? 回答1: A companion object is a static member of its surrounding class: public class Foo {

access to a type's companion object

♀尐吖头ヾ 提交于 2019-12-22 17:29:34
问题 I have a variable of some type and I'd like to get information from the companion object. For example, I thought I might be able to do something like this: def foo[I: Integral](i:I): = { val minVal = i match { case _:Byte => Byte.MinValue case _:Char => Char.MinValue case _:Int => Int.MinValue case _:Long => Long.MinValue case _:Short => Short.MinValue } // compare i and minVal } But this is rather verbose and minVal comes out as :Long which complicates comparisons with i: I . I was hoping I