@tailrec error in scala eclipse sheet: expected definition start

I am new to scala and now I practice on the worksheet. I noticed that @tailrec does not work on the sheet, although I added import

import scala.annotation.tailrec 

This is a version of scala, I am using

 Scala code runner version 2.10.2 -- Copyright 2002-2013, LAMP/EPFL 

Is there any way to make it work? Thanks

+6
source share
3 answers

The problem you described is a bug in the Eclipse IDE for Scala: https://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001636#/activity/ticket

The workaround is to put @tailrec in def or another object.

eg:.

 package tailrecfunc import scala.annotation.tailrec object Session17 { val block = { @tailrec def tailrecfunc(n: Int): Int = if(n == 0) n; else tailrecfunc(n - 1) tailrecfunc(4) } } 

This way the scala interpreter will warn you when a function is not tail recursive

+9
source

Be careful that you are not mistaken how the @tailrec annotation @tailrec - this does not force the compiler to optimize the function in a tail-recursive manner (the compiler will alwasys do this optimization anyway if it can).

Most likely, this is just a marker that you can use to tell the compiler: "I think this function was successfully written in a tail recursive manner, tell me if you can optimize it that way."

That is, use it where you want to be sure that you have correctly written a function designed to eliminate the tail, and the compiler can indicate when you make a mistake.

Does this mean what you see? I cannot say anything useful without seeing the sample code in which you used the annotation.

+4
source

First define and close the function you want to annotate, then go back and add the annotation.

+1
source

Source: https://habr.com/ru/post/954322/


All Articles