Scala Trait doubt


#1

Want to implement trait such that it has variables X and Y and functions add,multiply, division.
And there will be a class variable which will extend the trait and have a constructor taking two variable input and give the result upon call the corresponding function.

Kindly help…


#2

What have you tried so far? Do you have a code sample? It doesn’t have to be a working sample, just something to convey an idea of what you want to do.


#3

https://docs.scala-lang.org/tour/traits.html probably covers what you’re after.


#4

I am trying to fit the trait functionality as mentioned earlier in below code:

trait ArthOps //code to be written for trait
class Variables //code to be written
object TraitExample extends App{
val1 input= arg(0).toInt
val2 input=arg(0).toInt
var variable=new Variables (input1,input)
println(variable.add())
println(variable.subtract())
}


#5

Meta: you can enclose code samples in triple-backticks to format them properly with syntax highlighting.

Anyway, a few things:

  • ArthOps means ‘arithmetic ops’, right? I would recommend not introducing contractions unless they’re well-known in Scala :slight_smile: also, Ops classes are usually used for extension methods to existing classes. So, maybe go with just trait Arithmetic {...}.
  • It looks like Variables needs to extend Arithmetic
  • val1 input = ... is not valid Scala syntax, you’ll need val input1 = ... and so on
  • You don’t need a var (reassignable variable) here, you can use val variables = new Variables(input1, input2)
  • For methods that don’t take any arguments and just do pure calculations, it’s idiomatic in Scala to give them a nullary parameter list, in other words, don’t use parentheses for the add and subtract methods.
  • In fact I’d recommend not to call them add and subtract as those are verbs which indicate ‘I’m doing something here’; prefer to use nouns that describe the result of the call, e.g. sum and difference.
  • (Edit) the App trait will be deprecated in the future, it’s best to use a normal object and an explicit main method

Good luck!


#6

Hey, Were you able to get the solution for this…Im also looking for this and struck to proceed


#7

App trait is being deprecated?


#8

I spoke too soon, it might not be deprecated :man_facepalming:

But using App is still risky imho, as explained here: https://www.reddit.com/r/scala/comments/4zvelw/app_trait_vs_main_method/?st=jr19pctb&sh=e792f507


#9

That example on Reddit is not the best, since overriding main of App is deprecated since 2.11.0 and impossible since 2.13.0. But it does indeed cause initialization issues:

scala> abstract class FooPrinter extends App {
     |   val foo = 1
     |   println(foo)
     | }
defined class FooPrinter

scala> object FooPrinterImpl extends FooPrinter {
     |   override val foo = 2
     | }
defined object FooPrinterImpl

scala> FooPrinterImpl.main(Array())
0

scala> abstract class FooPrinter {
     |   val foo = 1
     |   final def main(args: Array[String]): Unit = println(foo)
     | }
defined class FooPrinter

scala> object FooPrinterImpl extends FooPrinter {
     |   override val foo = 2
     | }
defined object FooPrinterImpl

scala> FooPrinterImpl.main(Array())
2