Some kind of generics
Posted: Sat Sep 05, 2009 12:20 pm
I thought about how some kind of generic type system could be integrated into Purebasic
and this is what I came up with.
I know, this is a very bad example but I hope you get the idea. Similar to lists or maps
a type (= Structure) could be passed to a procedure which then could work with it
as if it was a variable. The SizeOf call is just an example, maybe one of you can
come up with something more suggestive.
The practical reason why I thought about generics is, I'm currently implementing
a custom data container which should work with all kind of datatypes. Of course
I could simply specify the size of the type as an argument, but this is kind of inelegant.
What do you think about this? Are there any pitfalls or problems I didn't take in account?
and this is what I came up with.
Code: Select all
Procedure AllocateStruct(Type struct)
Protected *Ptr.struct = AllocateMemory(SizeOf(struct))
ProcedureReturn Mem
EndProcedure
a type (= Structure) could be passed to a procedure which then could work with it
as if it was a variable. The SizeOf call is just an example, maybe one of you can
come up with something more suggestive.
The practical reason why I thought about generics is, I'm currently implementing
a custom data container which should work with all kind of datatypes. Of course
I could simply specify the size of the type as an argument, but this is kind of inelegant.
What do you think about this? Are there any pitfalls or problems I didn't take in account?