You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here is an example of code describing a possible syntax for use with templates (Vector comes to mind as a candidate)..
module test;
import stdlib;
public type Vector template X {
X* data;
u32 num_data;
u32 max_data;
}
public func void Vector.init(Vector* v, u32 capacity) {
// Note: we need cast if X is pointer type
v.data = cast<X*>(stdlib.malloc(capacity * sizeof(X)));
v.max_data = capacity;
v.num_data = 0;
}
public func void Vector.add(Vector* v, X x) {
if (v.num_data == v.max_data) {
// resize
}
X x1; // allowed
v.data[v.num_data] = x;
v.num_data++;
}
// Note: only analysed for instances, not in template form! (only parsed)
public func u32 Vector.size(const Vector* v) {
return v.num_data;
}
public func X Vector.get(const Vector* v, u32 idx) {
return v.data[idx];
}
// Template function, put in C2 namespace
func X max(X left, X right) template X {
if (left >= right) return left;
return right;
}
type PointerList Vector<void*>;
public func i32 main() {
// Need to modify TypeRef to store this
// To parse, need lookahead
Vector<u32> numbers;
Vector<i32*> pointers; // 1st option
PointerList plist; // 2nd option
numbers.init(4);
numbers.add(1);
i32 a = 1;
i64 b = 10;
// TODO: is this parseable? max < (basetype) vs max < a
i32 c = max<i32>(a, b); // get warning for type reduction i64 -> i32
return 0;
}
Note that C2C currently does not support templates
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is an example of code describing a possible syntax for use with templates (Vector comes to mind as a candidate)..
Note that C2C currently does not support templates
Beta Was this translation helpful? Give feedback.
All reactions