Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I override a scala class method with a method from a trait?

Tags:

scala

class PasswordCaseClass(val password:String)
trait PasswordTrait { self:PasswordCaseClass =>
    override def password = "blue"
}

val o = new PasswordCaseClass("flowers") with PasswordTrait

Is it possible to override PasswordCaseClass's password with what is provided in PasswordTrait? Right now, I receive this error:

e.scala:6: error: overriding value password in class PasswordCase
Class of type String;
 method password in trait PasswordTrait of type => java.lang.String needs to be a stable,
immutable value
val o = new PasswordCaseClass("flowers") with PasswordTrait
            ^
one error found

I would like to be able to have something like this:

class User(val password:String) {
}

trait EncryptedPassword { u:User =>
  def password = SomeCriptographyLibrary.encrypt(u.password)
}

val u = new User("random_password") with EncryptedPassword
println(u.password) // see the encrypted version here
like image 292
Geo Avatar asked Apr 25 '11 08:04

Geo


People also ask

Can Scala traits have methods?

In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.

What is method overriding in Scala?

When a subclass has the same name method as defined in the parent class, it is known as method overriding. When subclass wants to provide a specific implementation for the method defined in the parent class, it overrides method from parent class.

Can a trait take parameters Scala?

Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters. However, you can inherit (extend) them using classes and objects.

What is the difference between a trait and an abstract class in Scala?

Traits does not contain constructor parameters. Abstract class contain constructor parameters. Traits are completely interoperable only when they do not contain any implementation code. Abstract class are completely interoperable with Java code.

What is an example of method overriding in Scala?

These are the following Scala method overriding examples, let’s see them one by one: a. Example 1 In this example, class Student extends class Person. It overrides the method greet () from class Person to its own definition using the Scala ‘override’ keyword.

Can a class override a method defined in a trait?

A class can also override a method that’s defined in a trait. Here’s an example: class Cat extends Pet { // override 'speak' override def speak(): Unit = println("meow") def comeToMaster(): Unit = println("That's not gonna happen.")

What is a trait in Scala?

Introduction to Scala Trait Traits in Scala are like partially implemented interfaces. It may contain abstract and non-abstract methods. It may be that all methods are abstract, but it should have at least one abstract method.

Are all methods of a Scala class abstract?

It may be that all methods are abstract, but it should have at least one abstract method. Not only are they similar to Java interfaces, Scala compiles them into those with corresponding implementation classes holding any methods implemented in the traits. You can say that using Scala trait, we can share interfaces and fields between classes.


1 Answers

You can override a def with a val, but you can't do it the other way around. A val implies a guarantee -- that it's value is stable and immutable -- that a def does not.

like image 195
Daniel C. Sobral Avatar answered Oct 20 '22 08:10

Daniel C. Sobral