Tanton Gibbs
Posts: 20
Nickname: tanton
Registered: Aug, 2005
|
|
Re: Thinking out loud about Concepts
|
Posted: Aug 6, 2005 11:34 AM
|
|
You definitely don't want this to be a runtime error; it should be caught at compile time. In C++, we usually get an error like "Error on line x in MyFun, instantiated from line y with types IntStack, FooStack" That way, you get the entire error stack. For a staticly typed language, that shouldn't be a problem.
One question that I had when dealing with ConceptC was how to say that two concepts should represent the same type. For instance, in your MyFxn function, you have two Stacks that should both be of the same type. How do you specify this? Currently, if you pass in two different typed Stacks you (might/should/will) get a compiler error depending on the convertability of the Stacks, etc...
One thing to think about is allowing users to explicitly specify what type an Any member should bind to when writing a concept. This would be similar to a template, but the compiler would check to make sure the provided type is identical to the specified type. For instance:
void MyFxn( Stack[value_type=int]& x, Stack[value_type=int]& y ) { y.push(x.pop()); }
In this case, if you passed in a FloatStack, the compiler would complain; furthermore, it shows in the function specification what the type should be.
For syntax sugar, if the concept only has one Any type, then you could leave off the name= part.
void MyFxn( Stack[int]& x, Stack[int]& y ) { y.push(x.pop()); }
In both of these cases, calling MyFxn would result in an error if the type instantiating the concept did not provide a value_type member that was an int.
Secondly, what if you wanted both of them to be the same, but you didn't care what type they were? Perhaps something like:
void MyFxn( Stack[T1:Any]& x, Stack[T1]& y )
This says that both x and y are Stacks of Anys, but both must be the same (T1).
Thirdly, what if you had three types, two of which should be the same, and the third you didn't care about
void MyFxn( Stack[T1:Any]& x, Stack[T1]& y, Stack& z );
Finally, what if you had two types and wanted them both different types
void MyFxn( Stack[T1:Any]& x, Stack[T2:Any]& y );
You may have already solved this problem much more elegantly than this, but it has plagued me for quite some time.
|
|