In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the meaning of scala comments". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Annotations associate meta-information with a definition. For example, the annotation @ deprecated before the method causes the compiler to print a warning message when the method is used.
Object DeprecationDemo extends App {@ deprecated ("deprecation message", "release # which deprecates method") def hello = "hola"
Hello}
This program can be compiled, but the compiler will print a warning message: "there was one deprecation warning".
An annotation acts on the first definition or declaration that follows. There can be multiple comments before definition and declaration. The order of these comments is not important.
Comments to ensure the correctness of the code
If the conditions are not met, some annotations will actually cause the compilation to fail. For example, the annotation @ tailrec ensures that the method is tail recursive. Tail recursion can keep the memory requirements unchanged. The following is its use in the method of calculating factorial:
Import scala.annotation.tailrec
Def factorial (x: Int): Int = {
Tailrec def factorialHelper (x: Int, accumulator: Int): Int = {if (x = = 1) accumulator else factorialHelper (x-1, accumulator * x)} factorialHelper (x, 1)}
The method factorialHelper uses the annotation @ tailrec to ensure that the method is indeed tail recursive. If we change the implementation of method factorialHelper to the following, it will fail to compile:
Import scala.annotation.tailrec
Def factorial (x: Int): Int = {@ tailrec def factorialHelper (x: Int): Int = {if (x = = 1) 1 else x * factorialHelper (x-1)} factorialHelper (x)}
We will get an error message "Recursive call not in tail position".
Comments that affect code generation
Annotations like @ inline can affect the generated code (that is, your jar file may have different bytes than when you don't use annotations). Inline represents the code that is inserted into the body of the called method at the call point. The generated bytecode is longer, but hopefully it runs faster. The use of the annotation @ inline does not guarantee that the method is inline, and it triggers the compiler to do so if and only if it satisfies some heuristic algorithm of generated code size.
Java comments
When writing Scala code that interoperates with Java, there are some differences in annotation syntax. Note: make sure you use Java annotations when you turn on the-target:jvm-1.8 option.
Java annotations are in the form of user-defined metadata, refer to annotations. A key feature of annotations is that they rely on specifying name-value pairs to initialize their elements. For example, if we need an annotation to track the source of a class, we can define it as
@ interface Source {public String URL (); public String mail ();}
And use it as follows
Source (URL = "https://coders.com/", mail =" support@coders.com ") public class MyClass extends HisClass...
The annotation application in Scala looks like a constructor call, and to instantiate Java annotations, you must use named parameters:
Source (URL = "https://coders.com/", mail =" support@coders.com ") class MyScalaClass...
If the annotation contains only one element (there is no default value), this syntax is tedious, so by convention, if you specify the element name as value, you can apply it in Java using syntax similar to the constructor:
@ interface SourceURL {public String value (); public String mail () default "";}
Then use it as follows
@ SourceURL ("https://coders.com/")public class MyClass extends HisClass... In this case, Scala offers the same possibility
@ SourceURL ("https://coders.com/")class MyScalaClass...
The mail element is defined with a default value, so we don't need to explicitly provide a value for it. However, if we need to provide values explicitly, we cannot mix the two methods in Java:
SourceURL (value = "https://coders.com/", mail =" support@coders.com ") public class MyClass extends HisClass...
Scala provides greater flexibility in this regard
@ SourceURL ("https://coders.com/", mail =" support@coders.com ") class MyScalaClass..." what does the comment of scala mean? "that's it. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.