wraith-lang/generic.w

60 lines
1.2 KiB
OpenEdge ABL
Raw Permalink Normal View History

struct Foo {
static Func2<U>(u: U) : U {
return u;
}
static Func<T>(t: T): T {
foo: T = t;
return Foo.Func2(foo);
}
}
2021-06-03 00:26:26 +00:00
struct MemoryBlock<T>
{
start: MemoryAddress;
capacity: uint;
AddressOf(index: uint): MemoryAddress
2021-06-03 00:26:26 +00:00
{
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);
2021-06-03 00:26:26 +00:00
}
}
struct Program {
static Main(): int {
x: int = 4;
y: int = Foo.Func(x);
block: MemoryBlock<int> = MemoryBlock<int>
{
capacity: y,
start: @malloc(y * @sizeof<int>())
};
block.Set(0, 5);
block.Set(1, 3);
block.Set(2, 9);
block.Set(3, 100);
Console.PrintLine("%p", block.start);
Console.PrintLine("%i", block.Get(0));
Console.PrintLine("%i", block.Get(1));
Console.PrintLine("%i", block.Get(2));
Console.PrintLine("%i", block.Get(3));
block.Free();
return 0;
}
}