Is PositionMethods startLine 0-based intentionally? #12728
-
I'm using case class CallerPos(filename: String, line: Int) {
def render = s"$filename:$line"
}
object CallerPos {
inline given CallerPos = ${ deriveCallerPos }
def deriveCallerPos(using quotes: Quotes): Expr[CallerPos] = {
import quotes.reflect._
val pos = Position.ofMacroExpansion
val filename = Expr { pos.sourceFile.jpath.getFileName.toString }
val line = Expr { pos.startLine }
'{ CallerPos(${filename}, ${line}) }
}
} and I noticed that if I say e.g. The roughly-equivalent macro in scala 2.x reports line 118: object CallerPos {
implicit def capture: CallerPos = macro locationMacro
def locationMacro(c: blackbox.Context) = {
import c.universe._
val filename = c.enclosingPosition.source.file.name
val line = c.enclosingPosition.line
q"_root_.io.dylemma.spac.CallerPos($filename, $line)"
}
} I wasn't sure if this was intentional - should I just resort to adding a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yeah, I was tripped over this too. You could say that all editors are wrong and should have started with line 0 and column 0 😉 |
Beta Was this translation helpful? Give feedback.
-
This was intentional. The line/column indexing is 0-based. The documentation should be clearer about this. This is the position in the file which annoyingly is not the same as in editors. We chose this variant to avoid overhead in the reflection API implementation. |
Beta Was this translation helpful? Give feedback.
This was intentional. The line/column indexing is 0-based. The documentation should be clearer about this.
This is the position in the file which annoyingly is not the same as in editors. We chose this variant to avoid overhead in the reflection API implementation.