Cake pattern: mixing in in a trait

Loading...

Cake pattern: mixing in in a trait

I've been playing with the cake pattern and there's something I don't fully understand.
Given the following common code:
trait AServiceComponent {
  this: ARepositoryComponent =>
}

trait ARepositoryComponent {}

the following way of mixing them works
trait Controller {
  this: AServiceComponent =>
}

object Controller extends 
  Controller with 
  AServiceComponent with 
  ARepositoryComponent

But the following does not
trait Controller extends AServiceComponent {}

object Controller extends
  Controller with
  ARepositoryComponent

with error:
illegal inheritance; self-type Controller does not conform to AServiceComponent's selftype AServiceComponent with ARepositoryComponent

Shouldn't we be able to "push" dependencies up in the hierarchy if we know that they will be common for all subclasses?
Shouldn't the compiler allow for Controller to have dependencies, as long as it's not instantiated without resolving them?

Solutions/Answers:

Answer 1:

Here’s a slightly simpler way to run into the same issue:

scala> trait Foo
defined trait Foo

scala> trait Bar { this: Foo => }
defined trait Bar

scala> trait Baz extends Bar
<console>:9: error: illegal inheritance;
 self-type Baz does not conform to Bar's selftype Bar with Foo
       trait Baz extends Bar
                         ^

The problem is that the compiler expects you to repeat the self-type constraint in the subtype definition. In my simplified case we’d write:

trait Baz extends Bar { this: Foo => }

In yours you just need to make the following change:

trait Controller extends AServiceComponent { this: ARepositoryComponent => }

This requirement makes some sense—it’s reasonable to want someone using Controller to be able to know about this dependency without looking at the types it inherits from.

Our Awesome Tools

References

Loading...

IDEA 13 regex checker in Scala

Loading...

IDEA 13 regex checker in Scala

There is cool feature in IDEA to check your regex in Java.
When you hit Alt+Enter over your selected pattern, you get:

And if you choose Check RegExp, you get something like this:

But if you try same thing using Scala there is no Check RegExp option. I've tried hitting Alt+Enter for each of following lines of code:
val myRegex = """\w""".r
val myRegex2:Pattern = Pattern.compile("""\w""")
val myregex3 = new Regex("""\w""")

but no Check RegExp option showed. Any idea how to "enable" this feature to work with Scala?

Solutions/Answers:

Answer 1:

Expanding the tip of @ziggystar, if you have the IntelliLang plugin installed, you can “inject” Regex syntax into a string. There is a message though stating that the injection was temporary, and the injection doesn’t survive IntelliJ restart.

Injection screenshot

Our Awesome Tools

References

Loading...