Generic Class
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// No Native Support.
// Use void pointers, macros, or generate type-specific structs/functions.
typedef struct Box
{
void *value;
} Box;
template <typename T>
class Box
{
public:
T value;
};
Box<int> box {10};
class Box<T>
{
public T Value { get; set; }
}
var box = new Box<int> { Value = 10 };
class Box<T> {
private T value;
public T getValue() { return value; }
public void setValue(T value) { this.value = value; }
}
Box<Integer> box = new Box<>();
struct Boxed<T> {
value: T,
}
let boxed = Boxed { value: 10 };
// Since Go 1.18
type Box[T any] struct {
Value T
}
box := Box[int]{Value: 10}
// No Native Support.
// Javascript classes are dynamically typed.
class Box {
constructor(value) {
this.value = value;
}
}
class Box<T> {
constructor(public value: T) {}
}
const box = new Box<number>(10);
from typing import Generic, TypeVar
T = TypeVar("T")
class Box(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value
box: Box[int] = Box(10)