NEELAMADHAB
NEELAMADHAB

Reputation: 87

Scala Mixin with self=> traits

Suppose I have a base trait, and my traits are extending the base trait. I want to create a class with mix-in self=> trait and can be call the overloaded dynamically.

Upvotes: 1

Views: 422

Answers (1)

NEELAMADHAB
NEELAMADHAB

Reputation: 87

Below can be the Solution

package minxin.neel.test

    /**
      * Created by Neelamadhab.
     */
    class TestMix2 {
      def hello = println("Hello NEEL...")
    }

   class BaseMixInClass {
      self: BaseTrait =>
      self.method
   }

   trait BaseTrait {
      def method = println("Inside method in BaseTrait")
   } 

   trait Trait2 extends BaseTrait {
      override def method = println("Hello, Inside method in Trait2")
   }

trait Trait3 extends BaseTrait {
  override def method = println("Hello, Inside method in Trait3")
}

trait Trait4 extends BaseTrait


object MyMixinTest extends App {
  new BaseMixInClass with Trait2
  new BaseMixInClass with Trait3
  new BaseMixInClass with Trait4
  new BaseMixInClass with BaseTrait
}

The output will be :

Hello, Inside method in Trait2
Hello, Inside method in Trait3
Inside method in BaseTrait
Inside method in BaseTrait

Upvotes: 1

Related Questions