www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Auto return type inference issue?

reply "Matthew Dudley" <pontifechs gmail.com> writes:
Here's the gist of what I'm trying to do.

https://gist.github.com/pontifechs/11169069

I'm getting an error I don't understand:

tinker.d(42): Error: mismatched function return type inference of 
tinker.B and tinker.A
tinker.d(55): Error: template instance tinker.DynamicTuple!(A, 
B).DynamicTuple.notopDispatch!"one" error instantiating
Failed: 'dmd' '-v' '-o-' 'tinker.d' '-I.'

Also, as an aside, why can't tuples be indexed dynamically? Most 
of my problems with this have been because you apparently can't.
Apr 22 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Matthew Dudley:

 Also, as an aside, why can't tuples be indexed dynamically? 
 Most of my problems with this have been because you apparently 
 can't.
Think about how D tuples are implemented, they are essentially structs. Every tuple element can have a different type and to be represented in memory with a different number of bytes. D is statically typed so when you read an element of a tuple, you need to know at compile-time the type (and size) of what you are reading. To have tuples indexable at run-time their items need to be all of the same type (so they are essentially arrays), or they need to be some kind of variants (but later you need to manage their type more manually). Bye, bearophile
Apr 22 2014
prev sibling parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 22 April 2014 at 07:54:34 UTC, Matthew Dudley wrote:
 Here's the gist of what I'm trying to do.

 https://gist.github.com/pontifechs/11169069

 I'm getting an error I don't understand:

 tinker.d(42): Error: mismatched function return type inference 
 of tinker.B and tinker.A
 tinker.d(55): Error: template instance tinker.DynamicTuple!(A, 
 B).DynamicTuple.notopDispatch!"one" error instantiating
 Failed: 'dmd' '-v' '-o-' 'tinker.d' '-I.'

 Also, as an aside, why can't tuples be indexed dynamically? 
 Most of my problems with this have been because you apparently 
 can't.
Might have this a little bit wrong, just a quick hand translation of: foreach (i, elem; tuple) if (names[i] == s) { return elem; } This becomes something like: if (names[0] == "one") return A; if (names[1] == "one") return B; As said I could have this mixed up, but /elem/ is a type. But even if they were an object, you are saying that this method may return an A or a B, it is not legal for a function to return different types. I'd suggest a static if, but since /names/ is dynamic, that won't work.
Apr 22 2014
parent reply "Matthew Dudley" <pontifechs gmail.com> writes:
On Wednesday, 23 April 2014 at 00:02:29 UTC, Jesse Phillips wrote:
 On Tuesday, 22 April 2014 at 07:54:34 UTC, Matthew Dudley wrote:
 Here's the gist of what I'm trying to do.

 https://gist.github.com/pontifechs/11169069

 I'm getting an error I don't understand:

 tinker.d(42): Error: mismatched function return type inference 
 of tinker.B and tinker.A
 tinker.d(55): Error: template instance tinker.DynamicTuple!(A, 
 B).DynamicTuple.notopDispatch!"one" error instantiating
 Failed: 'dmd' '-v' '-o-' 'tinker.d' '-I.'

 Also, as an aside, why can't tuples be indexed dynamically? 
 Most of my problems with this have been because you apparently 
 can't.
Might have this a little bit wrong, just a quick hand translation of: foreach (i, elem; tuple) if (names[i] == s) { return elem; } This becomes something like: if (names[0] == "one") return A; if (names[1] == "one") return B; As said I could have this mixed up, but /elem/ is a type. But even if they were an object, you are saying that this method may return an A or a B, it is not legal for a function to return different types. I'd suggest a static if, but since /names/ is dynamic, that won't work.
tuple in this case would be the member variable of type T (from T...) So wouldn't elem be the actual object, and not the type? The effective lowering I was hoping for would be something like this if (names[0] == "one") return tuple[0]; if (names[1] == "one") return tuple[1]; etc...
Apr 23 2014
parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 23 April 2014 at 23:05:39 UTC, Matthew Dudley wrote:
 tuple in this case would be the member variable of type T (from 
 T...) So wouldn't elem be the actual object, and not the type?

 The effective lowering I was hoping for would be something like 
 this

 if (names[0] == "one")
     return tuple[0];
 if (names[1] == "one")
     return tuple[1];

 etc...
You're probably right, in which case both of those will be null and they are both different types. That is to say tuple[0] is A.init and tuple[1] is B.init of which a function must only return one type.
Apr 23 2014