• Forum
  • Lounge
  • Could it be also useful for functions to

 
Could it be also useful for functions to return a type..?

This popped into my head while I was looking at a thread earlier.
Would it be a useful addition to C++ (possibly in template programming), for a
function to be allowed to return a type (rather than an object of a type)
I mean something along the lines of:
1
2
3
4
5
6
template <typename T>
T some_function(params)
{
     //some code
     return float;
}


Could such a thing possibly be useful?
Last edited on
I can see why that would be useful (I can't think of any examples of it's usefulness off the top of my head though). I think it could be a useful addition to the language, although it might confuse people at first.
Type checking would be a nightmare. Maybe for a language with a looser type system.
How would you store that returned type? And what could you do with it that you can't do with a returned object (that already has a commonly known and used storage mechanism)?
Simple: types instances of metatypes. 'int' could be an instance of 'meta Integral' (a function returning an Integral is not the same as a function returning a meta Integral. One returns an instance of a derived class of Integral and the other returns a type). And the meta keyword could of course be piled so Three Star Programmers could do what they do best.
Although I'm wondering whether this would lead to an incompleteness (Gödel) problem.

It can be solved by having only abstract types be "metable". Since a metatype is not abstract (because it has instances), 'meta meta T' is not a valid type.

EDIT: But now, it's just polymorphism with a different syntax...
Last edited on
@helios,
You mean a type, e.g. metaint?
How would that help at all? You'd have to know the type anyway. You'd get an error trying to store an int in a metafloat, so you'd need to know that int could be stored in a metaint already.

I retract my previous statement. I can't think of any way this could help. gcc has typeof(), and I'm sure it's not the _only_ compiler that supports it.
I can think of one. Type casting, which can be done in a simple function.
Casting converts something to something of a different type. That's not the same as having types as first class objects.

Having types as objects would allow you to do something like
function(data) instance;
The problem is that function() can't return an arbitrary type in a language with strong typing, otherwise it's impossible to know whether the code that follows the declaration is consistent with the type being used:
1
2
3
function(data) instance=0.5f;
if (instance.substr(0,10)=="hello")
    instance=std::vector<lolwut>();

If you want to restrict the type that can be returned by the function, then it's conceptually identical to polymorphism:
1
2
3
std::istream *stream=function();
while (stream->peek()>=0)
    std::cout <<stream->get();
Now function() can return anything that has the same interface as std::istream. This is practically the same as returning a type.
Topic archived. No new replies allowed.