Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure exactly what you are trying to do, but this seems like a possible match to your intent:</p> <pre><code>trait C extends B { def myA = new A() with C { // Note that this could be "with B" rather than "with C" and it would still work. val name: String = C.this.name // explicit type not actually required - String type can be inferred. } } </code></pre> <p>then, for example:</p> <pre><code>scala&gt; val myC = new C() { val name = "myC-name" } myC: C = $anon$1@7f830771 scala&gt; val anA = myC.myA anA: A with C = C$$anon$1@249c38d5 scala&gt; val aName = anA.name aName: String = myC-name </code></pre> <p>Hopefully that can at least help guide your eventual solution. Might be able to give further help if you clarify what you want to do (or why you want to do it) further.</p> <p><strong>EDIT</strong> - after update to question:</p> <p>I suggest you may be thinking about this the wrong way. I would not like tying the Query class down to knowing how to forge a connection. Rather, either pass a ready-made connection as a parameter to your call that uses the connection, or (as shown below) set up the Query class to supply a function from a connection to a result, then call that function using a connection established elsewhere.</p> <p>Here is a how I would think about solving this (note that this example doesn't create an actual DB connection per se, I just take your DbConnection type at face value - really you have actually defined a 'DbConnConfig' type):</p> <pre><code>trait DbConnection { val dbName: String val dbHost: String } class Query(sql: String) { def doQuery: DbConnection =&gt; String = { conn: DbConnection =&gt; // No need here to know how to: connect using dbName, dbHost // perform query, using provided connection: s"${conn.dbName}-${sql}-${conn.dbHost}" // &lt;- Dummy implementation only here, so you can, for example, try it out in the REPL. } } class HasADbConnection(override val dbName: String, override val dbHost: String) extends DbConnection { // You can create your actual connection here... val conn = makeConnection def doSomething(query: Query) = { // ... or here, according to your program's needs. val conn = makeConnection query.doQuery(conn) } def makeConnection = this // Not a real implementation, just a quick cheat for this example. } </code></pre> <p>In reality, doQuery (which could be named better) should have a type of <code>DbConnection =&gt; ResultSet</code> or similar. Example usage:</p> <pre><code>scala&gt; val hasConn = new HasADbConnection("myDb", "myHost") hasConn: HasADbConnection = HasADbConnection@6c1e5d2f scala&gt; hasConn.doSomething(new Query("@")) res2: String = myDb-@-myHost </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload