wraith-lang/generic.w

84 lines
1.4 KiB
OpenEdge ABL

struct Foo {
static Func2<U>(u: U) : U {
return u;
}
static Func<T>(t: T): T {
foo: T = t;
return Foo.Func2(foo);
}
}
struct MemoryBlock<T>
{
start: MemoryAddress;
capacity: uint;
static Init(capacity: uint): MemoryBlock<T>
{
return MemoryBlock<T>
{
capacity: capacity,
start: @malloc(capacity * @sizeof<T>())
};
}
AddressOf(index: uint): MemoryAddress
{
return start + (index * @sizeof<T>());
}
Get(index: uint): T
{
return @dereference<T>(AddressOf(index));
}
Set(index: uint, value: T): void
{
@memcpy(AddressOf(index), @addr(value), @sizeof<T>());
}
Free(): void
{
@free(start);
}
}
struct Array<T>
{
memoryBlock: MemoryBlock<T>;
static Init(capacity: uint): Array<T>
{
return Array<T>
{
memoryBlock: MemoryBlock<T>.Init(capacity)
};
}
Get(index: uint): T
{
return memoryBlock.Get(index);
}
Set(index: uint, value: T): void
{
memoryBlock.Set(index, value);
}
}
struct Program {
static Main(): int {
array: Array<int> = Array<int>.Init(4);
x: int = 4;
y: int = Foo.Func(x);
array.Set(0, 2);
array.Set(1, 0);
array.Set(2, 5);
array.Set(3, 9);
Console.PrintLine("%i", array.Get(0));
Console.PrintLine("%i", array.Get(3));
return 0;
}
}