Introduce problems with native Java classes

I have the following problem: can someone help me (or explain where my error is). Given signs and class:

trait TextAttr[T <: {def setText(s:String); def getText : String}] {
  val obj : T
  def txt_= (s:String) = obj.setText(s)
  def txt = obj.getText
}

class ScalaText {
  private var t = ""
  def setText(s:String) = t = s
  def getText = t
}

Now I am creating a new class using both of them:

class ScalaTextUser extends TextAttr[ScalaText] {
  override val obj = new ScalaText
}

It's good. But if I want to create something similar with the class org.eclipse.swt.widgets.Text (or any other pure Java class) I get an error. Here is the code:

class SwtTextUser(parent:Composite) extends TextAttr[Text] {
  override val obj = new Text(parent, 0)
}

And this is a mistake:

arguments of type [org.eclipse.swt.widgets.Text] do not match the TextAttr type attribute of the parameter border [T <: AnyRef {def setText (String): Unit; def getText: String}]

Does anyone have an idea?

Thank you, Chris.

+3
source share
1 answer

Try changing the tag declaration to the following:

trait TextAttr[T <: {def setText(s: String); def getText(): String}] {

Scala - . , parens .

+2

Source: https://habr.com/ru/post/1711090/


All Articles