www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: cannot uniquely infer foreach argument types

reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
Hello, i'm trying to create a library with utilities classes like 
containers using Java API. Could anyone help me?

public interface Iterator(E) {
    bool hasNext();
    E next();
    void remove();
    int opApply(int delegate(ref E) delegation);
}

public class AbstractCollection(E) : Collection!E {

    .....

     E[] toArray(E[] dst = null) {
     	size_t count = size();
     	if( dst.length < count ) {
     		dst.length = count;
     	}
     	Iterator!E it = iterator();
     	foreach(int i,E e; it) { -> Error: cannot uniquely infer 
foreach argument types
     		dst[i] = e;
     	}
     	return dst[0 .. count];
     }

    .....

}
Jun 14 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Agustin:

 Hello, i'm trying to create a library with utilities classes 
 like containers using Java API.
The problem with this is that most Phobos works with ranges...
 Could anyone help me?
Maybe your code has multiple problems. If you want a precise answer, then give a complete little program. But a possible problem is in the opApply: int opApply(int delegate(ref E) delegation); If you want to use: foreach (i, e; it) { Then you need to put both in the opApply (or add a second opApply overload), something like: int opApply(int delegate(ref int, ref E) delegation); Bye, bearophile
Jun 14 2013
parent reply "Agustin" <agustin.l.alvarez hotmail.com> writes:
On Friday, 14 June 2013 at 17:17:46 UTC, bearophile wrote:
 Agustin:

 Hello, i'm trying to create a library with utilities classes 
 like containers using Java API.
The problem with this is that most Phobos works with ranges...
 Could anyone help me?
Maybe your code has multiple problems. If you want a precise answer, then give a complete little program. But a possible problem is in the opApply: int opApply(int delegate(ref E) delegation); If you want to use: foreach (i, e; it) { Then you need to put both in the opApply (or add a second opApply overload), something like: int opApply(int delegate(ref int, ref E) delegation); Bye, bearophile
int opApply(int delegate(ref int, ref E) delegation); Works!, thanks.
Jun 14 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 06/14/2013 10:25 AM, Agustin wrote:

 int opApply(int delegate(ref int, ref E) delegation);

 Works!, thanks.
I have just completed the translation of the "foreach with Structs and Classes" chapter: http://ddili.org/ders/d.en/foreach_opapply.html Ali
Jun 14 2013
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 14 Jun 2013 13:13:06 -0400, Agustin  
<agustin.l.alvarez hotmail.com> wrote:

 Hello, i'm trying to create a library with utilities classes like  
 containers using Java API. Could anyone help me?

 public interface Iterator(E) {
     bool hasNext();
     E next();
     void remove();
Here is the issue (as bearophile explained)
     int opApply(int delegate(ref E) delegation);
Also, you may want to try an already-created library that's similar to Java in some respects: http://schveiguy.github.io/dcollections/ -Steve
Jun 14 2013