खोज…


परिचय

मैक्रोज़ संकलन समय के एक रूप हैं, जो मेटाप्रोग्रामिंग हैं। स्केल कोड के कुछ तत्व, जैसे कि एनोटेशन और विधियाँ, अन्य कोड को बदलने के लिए बनाया जा सकता है जब वे संकलित किए जाते हैं। मैक्रोज़ साधारण स्काला कोड होते हैं जो अन्य प्रकार का प्रतिनिधित्व करने वाले डेटा प्रकारों पर काम करते हैं। [मैक्रो पैराडाइज] [] प्लगइन बेस भाषा से परे मैक्रोज़ की क्षमताओं का विस्तार करता है। [मैक्रो पैराडाइज़]: http://docs.scala-lang.org/overviews/macros/paradise.html

वाक्य - विन्यास

  • def x () = मैक्रो x_impl // x एक मैक्रो है, जहां कोड बदलने के लिए x_impl का उपयोग किया जाता है
  • def मैक्रोट्रांसफॉर्म (एनोटेट: एनी *): एनी = मैक्रो इम्पेट // इनका उपयोग मैक्रो बनाने के लिए एनोटेशन में करें

टिप्पणियों

मैक्रोज़ एक ऐसी भाषा सुविधा है जिसे सक्षम करने की आवश्यकता है, या तो scala.language.macros को आयात scala.language.macros या संकलक विकल्प के साथ- scala.language.macros -language:macros । केवल स्थूल परिभाषाओं के लिए यह आवश्यक है; मैक्रो का उपयोग करने वाले कोड को इसकी आवश्यकता नहीं है।

मैक्रो एनोटेशन

यह सरल मैक्रो एनोटेशन-के रूप में एनोटेट आइटम को आउटपुट करता है।

import scala.annotation.{compileTimeOnly, StaticAnnotation}
import scala.reflect.macros.whitebox.Context

@compileTimeOnly("enable macro paradise to expand macro annotations")
class noop extends StaticAnnotation {
  def macroTransform(annottees: Any*): Any = macro linkMacro.impl
}

object linkMacro {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._

    c.Expr[Any](q"{..$annottees}")
  }
}

@compileTimeOnly एनोटेशन एक संदेश के साथ एक त्रुटि उत्पन्न करता है जो दर्शाता है कि इस मैक्रो का उपयोग करने के लिए paradise संकलक प्लगइन को शामिल किया जाना चाहिए। एसबीटी के माध्यम से इसे शामिल करने के निर्देश यहां दिए गए हैं

आप इस तरह के ऊपर परिभाषित मैक्रो का उपयोग कर सकते हैं:

@noop
case class Foo(a: String, b: Int)

@noop
object Bar {
  def f(): String = "hello"
}

@noop
def g(): Int = 10

विधि मैक्रोज़

जब किसी विधि को मैक्रो के रूप में परिभाषित किया जाता है, तो संकलक कोड को ले जाता है जिसे उसके तर्क के रूप में पारित किया जाता है और इसे एएसटी में बदल देता है। फिर यह उस एएसटी के साथ मैक्रो कार्यान्वयन को लागू करता है, और यह एक नया एएसटी लौटाता है जिसे फिर इसकी कॉल साइट पर वापस भेज दिया जाता है।

import reflect.macros.blackbox.Context

object Macros {
  // This macro simply sees if the argument is the result of an addition expression.
  // E.g. isAddition(1+1) and isAddition("a"+1).
  // but !isAddition(1+1-1), as the addition is underneath a subtraction, and also
  // !isAddition(x.+), and !isAddition(x.+(a,b)) as there must be exactly one argument.
  def isAddition(x: Any): Boolean = macro isAddition_impl

  // The signature of the macro implementation is the same as the macro definition,
  // but with a new Context parameter, and everything else is wrapped in an Expr.
  def isAddition_impl(c: Context)(expr: c.Expr[Any]): c.Expr[Boolean] = {
    import c.universe._ // The universe contains all the useful methods and types
    val plusName = TermName("+").encodedName // Take the name + and encode it as $plus
    expr.tree match { // Turn expr into an AST representing the code in isAddition(...)
      case Apply(Select(_, `plusName`), List(_)) => reify(true)
      // Pattern match the AST to see whether we have an addition
      // Above we match this AST
      //             Apply (function application)
      //            /     \
      //         Select  List(_) (exactly one argument)
      // (selection ^ of entity, basically the . in x.y)
      //      /          \
      //    _              \
      //               `plusName` (method named +)
      case _                                     => reify(false)
      // reify is a macro you use when writing macros
      // It takes the code given as its argument and creates an Expr out of it
    }
  }
}

यह भी संभव है कि मैक्रोज़ तर्कों के रूप में Tree एस को लें। जैसे Expr s बनाने के लिए reify का उपयोग कैसे किया जाता है, q (quasiquote के लिए) स्ट्रिंग इंटरपोलर हमें Tree एस बनाने और डिकॉन्स्ट्रक्ट करने देता है। ध्यान दें कि हम ऊपर q उपयोग कर सकते थे ( expr.tree है, आश्चर्य, एक Tree ही) भी, लेकिन प्रदर्शनकारी उद्देश्यों के लिए नहीं।

// No Exprs, just Trees
def isAddition_impl(c: Context)(tree: c.Tree): c.Tree = {
  import c.universe._
  tree match {
    // q is a macro too, so it must be used with string literals.
    // It can destructure and create Trees.
    // Note how there was no need to encode + this time, as q is smart enough to do it itself.
    case q"${_} + ${_}" => q"true"
    case _              => q"false"
  }
}

मैक्रों में त्रुटियां

मैक्रोज़ अपने Context के उपयोग के माध्यम से संकलक चेतावनी और त्रुटियों को ट्रिगर कर सकते हैं।

जब हम खराब कोड की बात करते हैं, तो हम विशेष रूप से अति उत्साही होते हैं, और हम तकनीकी ऋण के प्रत्येक उदाहरण को संकलक सूचना संदेश के साथ चिह्नित करना चाहते हैं (चलो यह विचार कितना बुरा है इस बारे में नहीं सोचते हैं)। हम एक मैक्रो का उपयोग कर सकते हैं जो इस तरह के संदेश को छोड़ने के अलावा कुछ नहीं करता है।

import reflect.macros.blackbox.Context

def debtMark(message: String): Unit = macro debtMark_impl
def debtMarkImpl(c: Context)(message: c.Tree): c.Tree = {
  message match {
    case Literal(Constant(msg: String)) => c.info(c.enclosingPosition, msg, false)
    // false above means "do not force this message to be shown unless -verbose"
    case _                              => c.abort(c.enclosingPosition, "Message must be a string literal.")
    // Abort causes the compilation to completely fail. It's not even a compile error, where
    // multiple can stack up; this just kills everything.
  }
  q"()" // At runtime this method does nothing, so we return ()
}

इसके अतिरिक्त, उपयोग करने के बजाय ??? अनिमित कोड को चिह्नित करने के लिए, हम दो मैक्रोज़ बना सकते हैं, !!! और ?!? , कि एक ही उद्देश्य की सेवा, लेकिन संकलक चेतावनी का उत्सर्जन करें। ?!? एक चेतावनी जारी करने का कारण होगा, और !!! एक सटीक त्रुटि का कारण होगा।

import reflect.macros.blackbox.Context

def ?!? : Nothing = macro impl_?!?
def !!! : Nothing = macro impl_!!!

def impl_?!?(c: Context): c.Tree = {
  import c.universe._
  c.warning(c.enclosingPosition, "Unimplemented!")
  q"${termNames.ROOTPKG}.scala.Predef.???"
  // If someone were to shadow the scala package, scala.Predef.??? would not work, as it
  // would end up referring to the scala that shadows and not the actual scala.
  // ROOTPKG is the very root of the tree, and acts like it is imported anew in every
  // expression. It is actually named _root_, but if someone were to shadow it, every
  // reference to it would be an error. It allows us to safely access ??? and know that
  // it is the one we want.
}

def impl_!!!(c: Context): c.Tree = {
  import c.universe._
  c.error(c.enclosingPosition, "Unimplemented!")
  q"${termNames.ROOTPKG}.scala.Predef.???"
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow