March 18

To solve the problem with the 1-variable and 2-variable versions of foreach I
tried opApply and found that the compiler prefers it over opSlice and opIndex() (the latter without argument).

My code:

	int opApply(int delegate(Variant) foreachblock) const {
		int result = 0;
		foreach(val; dct) {
			result = foreachblock(val);
			if (result)
				break;
			}
		return result;
	}
	int opApply(int delegate(Variant, Variant) foreachblock) const {
		int result = 0;
		foreach(key, val; dct) {
			result = foreachblock(key, val);
			if (result)
				break;
		}
		return result;
	}

So I'm fine with this now.

March 18

@bachmeier

>

You're not the first one. There's no technical reason for the restriction. It's simply a matter of being opposed by those who make these decisions on the basis that it's the wrong way to program or something like that. Here is a recent thread: https://forum.dlang.org/post/ikwphfwevgnsxmdfqqkl@forum.dlang.org

Thank you for this. Very interesting discussion. And apparently a deliberate restriction of flexibility in type conversion.

I will first try to understand better how templates work under the hood before joining this discussion.

Given the types S and T in say templfunc(S, T)(S arg1, T arg2) {}
represent 2 different actual types in the program, does that mean that there are 4 versions of the templfunc function compiled in? (This was the C++ way iirc).
Or are the types T and S are put on the stack like ordinary arguments and the usage of arg1 and arg2 within the function is enveloped in switches that query these Types?

March 18

On Monday, 18 March 2024 at 08:50:42 UTC, rkompass wrote:

>

Given the types S and T in say templfunc(S, T)(S arg1, T arg2) {}
represent 2 different actual types in the program, does that mean that there are 4 versions of the templfunc function compiled in? (This was the C++ way iirc).

IMHO, only if you instantiate (make call) templfunc with all types.

>

Or are the types T and S are put on the stack like ordinary arguments and the usage of arg1 and arg2 within the function is enveloped in switches that query these Types?

IMHO, only if you instantiate (make call) templfunc, then compiler create function with specified types.
Function created only once for given types combination.

March 18

On Monday, 18 March 2024 at 10:05:43 UTC, novice2 wrote:

>

On Monday, 18 March 2024 at 08:50:42 UTC, rkompass wrote:

>

Or are the types T and S are put on the stack like ordinary arguments and the usage of arg1 and arg2 within the function is enveloped in switches that query these Types?

IMHO, only if you instantiate (make call) templfunc, then compiler create function with specified types.
Function created only once for given types combination.

this also can be helpfull:
https://dlang.org/spec/template.html#common_instantiation

1 2
Next ›   Last »