In C ++, I can create an instance of a generic type at compile time, and then build it at runtime:
struct Vertex {};
struct Edge {};
template<typename NodeType, typename IdType>
struct Wrapper {
IdType id;
Wrapper(IdType id) : id{id} {};
};
int main() {
using vertexWrapper = Wrapper<Vertex, int>;
vertexWrapper v(3);
}
Variables are clearly separated and types never look / don't look like values. I am trying to do something like this in the Chapel:
record Vertex {}
record Edge {}
record Wrapper {
type nodeType;
type idType;
var id: idType;
proc init(id) {
this.id = id;
}
}
type vertexWrapper = Wrapper(Vertex, int);
var v1 = 3: vertexWrapper;
When I compile this code, I get:
chpl -o graph-add-inclusion --cc-warnings test.chpl
test.chpl:9: In initializer:
test.chpl:10: error: can't omit initialization of field "nodeType", no type or default value provided
test.chpl:10: error: can't omit initialization of field "idType", no type or default value provided
Is there a way to separate type construction from value construction in the Chapel in order to achieve the effect of the tagged type that I am trying to get in my example? I use tagged types to have a single implementation that is common to two types of entities (vertices and edges here), but I want these objects to be different types.
There is one more related question. Should I just write:
type vertexWrapper = Wrapper(Vertex);
and then the integer is output separately from my constructor?
, , . , , , , ?