Attributes, Annotations & Decorators
This section is focused on specific examples (like the Deprecated/Obsolete) and how to create your own Attributes, Annotations or Decorators.
For Class Specific Annotations/Decorators like "Override", "Staticmethod" and so on, check the Class Section.
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// No Standard Attribute System before C23.
// Compilers commonly provide extensions such as GCC attributes.
__attribute__((deprecated))
void old_function()
{
// ...
}
__attribute__((unused))
static int my_variable;
More Info:
// ------------------------------------
// Deprecated Attribute (since C++14)
// ------------------------------------
// Options:
// [[deprecated]]
// [[deprecated( string message )]]
// Function example
[[deprecated]]
void my_function(void)
{
// ...
}
// Class example
class [[deprecated("Use another class. This one will be removed next version")]] Myclass
{
// ...
};
// ------------------------------------
// Custom Attributes
// ------------------------------------
// Standard C++ Doesnt Support Custom Attributes yet
More Info:
// ------------------------------------
// Obsolete Attribute
// ------------------------------------
//[Obsolete]
[Obsolete]
public void MyFunction()
{
// ...
}
//[Obsolete(string message)]
[Obsolete("Use another class. This one will be removed next version")]
public class MyClass
{
//...
}
//[Obsolete(string message, bool error)] (error == true means compilation error instead of warning)
[Obsolete("Use another class. This one wont compile", true)]
public class MyClass
{
//...
}
// ------------------------------------
// Other Standard Attributes
// ------------------------------------
[Serializable]
public class User
{
// ...
}
[Flags]
public enum FileAccess
{
Read = 1,
Write = 2,
Execute = 4
}
[Conditional("DEBUG")]
public void LogDebug(string message)
{
// ...
}
// ------------------------------------
// Custom Attributes
// ------------------------------------
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DocumentationAttribute : Attribute
{
public string Message { get; }
public DocumentationAttribute(string message)
{
Message = message;
}
}
[Documentation("Used by the syntax reference examples")]
public class MyClass
{
// ...
}
// ------------------------------------
// @Deprecated Annotation
// ------------------------------------
// The deprecated message can be documented in the Javadoc @deprecated tag.
/*
* @deprecated Use another method
*/
@Deprecated
public void MyMethod()
{
// ...
}
// ------------------------------------
// Other Standard Annotations
// ------------------------------------
@Override
public String toString()
{
return "MyClass";
}
@SuppressWarnings("unchecked")
public void myMethod()
{
// ...
}
@FunctionalInterface
public interface Operation
{
int apply(int first, int second);
}
// ------------------------------------
// Custom Annotation
// ------------------------------------
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Documentation
{
String value();
}
@Documentation("Used by the syntax reference examples")
public class MyClass
{
// ...
}
More Info:
#[derive(Debug, Clone)]
struct User {
name: String,
}
#[deprecated(note = "Use new_function instead")]
fn old_function() {
// ...
}
#[allow(dead_code)]
fn helper() {
// ...
}
// Go has no general attribute/annotation syntax.
// Struct tags are metadata strings used by packages such as encoding/json.
type User struct {
Name string `json:"name"`
Age int `json:"age,omitempty"`
}
// Compiler directives exist as comments for specific toolchain behavior.
//go:noinline
func myFunction() {
// ...
}
// Decorators are still an experimental feature
// There is a workaround package: https://www.npmjs.com/package/depd
// Decorators are still an experimental feature
// There is a workaround package: https://www.npmjs.com/package/depd
# ------------------------------------
# Deprecated Decorator
# ------------------------------------
# from the lib https://pypi.org/project/Deprecated/
from deprecated import deprecated
@deprecated(version='1.2.1', reason="You should use another function")
def some_old_function(x, y):
return x + y
class SomeClass(object):
@deprecated(version='1.3.0', reason="This method is deprecated")
def some_old_method(self, x, y):
return x + y
# ------------------------------------
# Custom Decorator
# ------------------------------------
from functools import wraps
def trace(function):
@wraps(function)
def wrapper(*args, **kwargs):
print(f"Calling {function.__name__}")
return function(*args, **kwargs)
return wrapper
@trace
def add(first, second):
return first + second