Skip to content

Commit f23b1c5

Browse files
Update Attributes, Annotations & Decorators Topic
1 parent bb32534 commit f23b1c5

6 files changed

Lines changed: 123 additions & 8 deletions

File tree

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
```C
2-
// TODO
2+
// No Standard Attribute System before C23.
3+
// Compilers commonly provide extensions such as GCC attributes.
4+
5+
__attribute__((deprecated))
6+
void old_function()
7+
{
8+
// ...
9+
}
10+
11+
__attribute__((unused))
12+
static int my_variable;
313
```
414
> More Info:
515
> - http://unixwiz.net/techtips/gnu-c-attributes.html

docs/programming-languages/attributes-annotations-decorators/_csharp.mdx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,50 @@ public class MyClass
2626
```
2727

2828
```Cs
29-
// TODO: Examples
29+
// ------------------------------------
30+
// Other Standard Attributes
31+
// ------------------------------------
32+
33+
[Serializable]
34+
public class User
35+
{
36+
// ...
37+
}
38+
39+
[Flags]
40+
public enum FileAccess
41+
{
42+
Read = 1,
43+
Write = 2,
44+
Execute = 4
45+
}
46+
47+
[Conditional("DEBUG")]
48+
public void LogDebug(string message)
49+
{
50+
// ...
51+
}
3052
```
3153

3254
```Cs
3355
// ------------------------------------
3456
// Custom Attributes
3557
// ------------------------------------
3658
37-
// TODO: Examples
59+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
60+
public class DocumentationAttribute : Attribute
61+
{
62+
public string Message { get; }
63+
64+
public DocumentationAttribute(string message)
65+
{
66+
Message = message;
67+
}
68+
}
69+
70+
[Documentation("Used by the syntax reference examples")]
71+
public class MyClass
72+
{
73+
// ...
74+
}
3875
```
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
```Go
2-
// TODO
2+
// Go has no general attribute/annotation syntax.
3+
// Struct tags are metadata strings used by packages such as encoding/json.
4+
5+
type User struct {
6+
Name string `json:"name"`
7+
Age int `json:"age,omitempty"`
8+
}
9+
10+
// Compiler directives exist as comments for specific toolchain behavior.
11+
//go:noinline
12+
func myFunction() {
13+
// ...
14+
}
315
```

docs/programming-languages/attributes-annotations-decorators/_java.mdx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,46 @@ public void MyMethod()
1616
```
1717

1818
```Java
19-
// TODO: Examples
19+
// ------------------------------------
20+
// Other Standard Annotations
21+
// ------------------------------------
22+
23+
@Override
24+
public String toString()
25+
{
26+
return "MyClass";
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
public void myMethod()
31+
{
32+
// ...
33+
}
34+
35+
@FunctionalInterface
36+
public interface Operation
37+
{
38+
int apply(int first, int second);
39+
}
2040
```
2141

2242
```Java
2343
// ------------------------------------
2444
// Custom Annotation
2545
// ------------------------------------
2646

27-
// TODO: Examples
47+
@Retention(RetentionPolicy.RUNTIME)
48+
@Target({ ElementType.TYPE, ElementType.METHOD })
49+
public @interface Documentation
50+
{
51+
String value();
52+
}
53+
54+
@Documentation("Used by the syntax reference examples")
55+
public class MyClass
56+
{
57+
// ...
58+
}
2859
```
2960

3061
> More Info:

docs/programming-languages/attributes-annotations-decorators/_python.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,17 @@ class SomeClass(object):
2323
# Custom Decorator
2424
# ------------------------------------
2525

26-
# TODO: Examples
26+
from functools import wraps
27+
28+
def trace(function):
29+
@wraps(function)
30+
def wrapper(*args, **kwargs):
31+
print(f"Calling {function.__name__}")
32+
return function(*args, **kwargs)
33+
34+
return wrapper
35+
36+
@trace
37+
def add(first, second):
38+
return first + second
2739
```
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
```Rust
2-
// TODO
2+
#[derive(Debug, Clone)]
3+
struct User {
4+
name: String,
5+
}
6+
7+
#[deprecated(note = "Use new_function instead")]
8+
fn old_function() {
9+
// ...
10+
}
11+
12+
#[allow(dead_code)]
13+
fn helper() {
14+
// ...
15+
}
316
```

0 commit comments

Comments
 (0)