Thread overview
Error: cannot uniquely infer foreach argument types
Jun 14, 2013
Agustin
Jun 14, 2013
bearophile
Jun 14, 2013
Agustin
Jun 15, 2013
Ali Çehreli
June 14, 2013
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];
    }

   .....

}
June 14, 2013
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
June 14, 2013
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.
June 14, 2013
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
June 15, 2013
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