Dotty: Failed to make a 3-slice cake in Dotty

Created on 18 May 2017  路  4Comments  路  Source: lampepfl/dotty

I made a 3-layer cake in Scala:

trait Toolbox extends Trees

trait Trees extends ValDefs with DefDefs { self: Toolbox => }

trait ValDefs { this: Trees => }

trait DefDefs { this: Trees => }

When I try to make the same cake in Dotty, I get the following message.

-- Error: examples/cake.scala:5:6 ----------------------------------------------
5 |trait ValDefs { this: Trees => }
  |      ^
  |missing requirement: self type Trees & ValDefs of trait ValDefs does not conform to self type Toolbox of required trait Trees
-- Error: examples/cake.scala:7:6 ----------------------------------------------
7 |trait DefDefs { this: Trees => }
  |      ^
  |missing requirement: self type Trees & DefDefs of trait DefDefs does not conform to self type Toolbox of required trait Trees

two errors found

As the cake is tasty, it would be nice to be able to make the same cake in Dotty.

(Note: I checked the code in RefChecks, in checkSelfConforms it doesn't check if the current class is a trait. Is this an oversight or intended behaviour?)

question

Most helpful comment

@liufengyun Here's how you can get it to compile:

trait Toolbox extends Trees

trait Trees extends ValDefs with DefDefs { self: Toolbox => }

trait ValDefs { this: Trees with Toolbox => }

trait DefDefs { this: Trees with Toolbox => }

Basically, the self-type of your trait has to be a subtype of the self-types of every type in your self-type. I agree that the error message could be clearer.

All 4 comments

@liufengyun Just a minor point on terminology (though the definitions are all very vague!): I think the traits being mixed in are "slices" rather than "layers". The "layers" are nested traits/classes which can be mixed-in in a structure which often mirrors that of their parent. I started experimenting with this, and discovered #2473.

I'll update the issue title, mainly out of my own pedantry! ;)

That's a new requirement in dotty. You need to mine the issues to find out where it was introduced exactly. I seem to remember it was for an important reason, though.

It should be easy to change the code so that it compiles in dotty.

@liufengyun Here's how you can get it to compile:

trait Toolbox extends Trees

trait Trees extends ValDefs with DefDefs { self: Toolbox => }

trait ValDefs { this: Trees with Toolbox => }

trait DefDefs { this: Trees with Toolbox => }

Basically, the self-type of your trait has to be a subtype of the self-types of every type in your self-type. I agree that the error message could be clearer.

Thanks a lot @smarter .

Was this page helpful?
0 / 5 - 0 ratings