-1

I have come across this new method definition. Need explanation what exactly happens here.

Parent trait

sealed trait Generic{
 def name : String = name  // what is the body of this function call?
 def id : Int = id         
 def place : String = place
}

Child case classes

case class Capital( 
  countryName : String, 
  override val id: Int, 
  override val place:String
) extends Generic
  • warning: method place in trait Generic does nothing other than call itself recursively I get this warning message is there anything wrong in using these types of methods?
  • How exactly compiler treat these type of function calls def name : String = name?
  • Is it this call treats its body as its method name?
8
  • 2
    My guess is whoever wrote that is being cute. Trait members that are not overridden are given default values of infinite recursion, which is OK only if they are never referenced. Commented Mar 7, 2018 at 8:51
  • @jwvh Being cute? that's sugar coding what I had in mind :) Commented Mar 7, 2018 at 9:28
  • @jwvh I wrote that code it is working perfectly well and want to know what is wrong with that syntax and how that syntax works? For your reference stackoverflow.com/questions/49134848/… Commented Mar 7, 2018 at 9:40
  • And I wrote that it is "infinite recursion," which is the answer to both of your questions: How does the syntax work? The method does nothing but call itself. What's wrong with it? Nothing, if what you want is a Stack Overflow exception when the method is called. If that's not what you want then that's what's wrong with it. Commented Mar 7, 2018 at 9:59
  • 1
    @PuneethReddyV if you don't know how to implement it, then why do you want to have a method body in a trait in the first place? Commented Mar 7, 2018 at 10:10

2 Answers 2

1

You are providing default implementations in the trait that are infinite loops, very much like in the following example:

def infiniteLoop: Unit = infiniteLoop

This is arguably the most useless and dangerous code that you could possibly put in a method of a trait. You could only make it worse by making it non-deterministic. Fortunately, the compiler gives you a very clear and precise warning:

warning: method place in trait Generic does nothing other than call itself recursively

  • "Is there anything wrong in using these types of methods"?: having unproductive infinite loops in your code is usually considered wrong, unless your goal is to produce as much heat as possible using computer hardware.
  • "How exactly compiler treat these type of function calls"?: Just like any other tail recursive function, but additionally it outputs the above warning, because it sees that it is obviously not what you want.
  • "Is it this call treats its body as its method name?": The body of each method declaration is what follows the =-sign. In your case, the otherwise common curly braces around the function body are omitted, and the entire function body consists only of the recursive call to itself.

If you don't want to have any unnecessary infinite loops around, simply leave the methods unimplemented:

sealed trait Generic{
  def name: String
  def id: Int
  def place: String
}

This also has the additional advantage that the compiler can warn you if you forget to implement one of these methods in a subclass.

Sign up to request clarification or add additional context in comments.

Comments

0

Ok, so in your trait you define methods body via recursion. Means that these methods, if not overridden (and they should not as soon as you have defined them somehow), will call itself recursively till StackOverflowError happens. For example, you did not override name method in Capital, so in this case you get StackOverflowError at runtime:

val c = Capital("countryName", 1, "place")
c.name

So, you are warned, that you have recursive definition. Trait is sealed, so at least it cannot be overridden in other places, but anyway, such definition is something like put mines on your road and rely on your memory, that you will not forget about them (and anybody else will be care enough to check trait definition before extending)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.