Scala, Maven and preprocessors

I know all the philosophical arguments against preprocessors and macros in Java. I do not agree that some of them may abuse the language function, they should be excluded for everyone.

I would like to enable macros __FILE__and __LINE__in my Java and Scala for effective logging. Any use of an exception is unacceptable due to performance impact at runtime. Those people who claim that registration can be turned off in the "production code" should follow Brian Kernigan’s advice:

Removing the "now that the program is running" error messages is like carrying a parachute on the ground, but removing it when you are in the air.

Is it likely that these macros can turn into a language? If not, is there a way to start a preprocessor like m4 using Maven?

Thank.

+3
source share
4 answers

Update @ Ralph Well, this is a question from 2+ years ago, but since I have the same need for __LINE__and __FILE__, and you mentioned SCALA; here are some ideas that I have using Scala macros (from version v2.10.0-RC1)

def $currentPosition:String = macro _currentPosition;
def _currentPosition(c:Context):c.Expr[String]={ import c.universe._;
  val pos = c.enclosingPosition;
  c.Expr(Literal(Constant(
    s"${pos.source.path}: line ${pos.line}, column ${pos.column}" )))
}

Macros, estimated at compile time , $currentPositionreplaced by a literal string that describes its position in the source code. For example, enter line printlnon line 13, it will display:

/sandbox/tmp_juno_workspace2/LogMacro_Test/src/test/Trial.scala: line 13, column 15

, , , ( , - !).

+1

"" - , ?

+1

. , , .

0

API , . "" / , . Sun/Oracle .

http://projectlombok.org. API , .. , , .

- :

public class Foo {
  @FileName private static String fileName;
  @LineNumber private static int lineNumber;
  ...
  public void foo() {
     log(fileName, lineNumber, "some message");
  }
}

fileName lineNumber /.

, JDK. , Sun/Oracle " , ", .

0

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


All Articles