Jump to page: 1 2 3
Thread overview
How to call opCall as template?
Jan 30, 2014
Namespace
Jan 30, 2014
Stanislav Blinov
Jan 30, 2014
Namespace
Jan 30, 2014
Frustrated
Jan 30, 2014
Namespace
Jan 30, 2014
Frustrated
Jan 30, 2014
Meta
Jan 30, 2014
Frustrated
Jan 30, 2014
Namespace
Jan 30, 2014
Frustrated
Jan 30, 2014
Frustrated
Jan 30, 2014
Namespace
Jan 30, 2014
Frustrated
Jan 30, 2014
Namespace
Jan 30, 2014
Frustrated
Jan 31, 2014
Namespace
Jan 31, 2014
Stanislav Blinov
Jan 31, 2014
Frustrated
Jan 31, 2014
Namespace
Jan 31, 2014
Frustrated
Jan 31, 2014
Namespace
Jan 31, 2014
Frustrated
Jan 30, 2014
Namespace
January 30, 2014
Here: http://dlang.org/operatoroverloading.html#FunctionCall
is this example:
----
import std.stdio;

struct F {
	int opCall() {
		return 0;
	}
	
	int opCall(int x, int y, int z) {
		return x * y * z;
	}
}

void main() {
	F f;
	int i;

	i = f();      // same as i = f.opCall();
	i = f(3,4,5); // same as i = f.opCall(3,4,5);
}
----

And it works of course. But what if I want to templatize opCall? How can I call it?

----
import std.stdio;

struct F {
	T opCall(T = int)(int a, int b, int c) {
		return cast(T)(a * b * c);
	}
}

void main() {
	F f;
	int i = f(3,4,5);
	float f_ = f!float(6, 7, 8);
}
----

Does not work, it fails with:
Error: template instance f!float f is not a template declaration, it is a variable
January 30, 2014
> void main() {
> 	F f;
> 	int i = f(3,4,5);
> 	float f_ = f!float(6, 7, 8);
> }
> ----
>
> Does not work, it fails with:
> Error: template instance f!float f is not a template declaration, it is a variable

f.opCall!float(6, 7, 8);
January 30, 2014
On Thursday, 30 January 2014 at 16:24:00 UTC, Stanislav Blinov wrote:
>> void main() {
>> 	F f;
>> 	int i = f(3,4,5);
>> 	float f_ = f!float(6, 7, 8);
>> }
>> ----
>>
>> Does not work, it fails with:
>> Error: template instance f!float f is not a template declaration, it is a variable
>
> f.opCall!float(6, 7, 8);

... Yes, of course. But where is the sense to use opCall if I need to call it explicitly?
January 30, 2014
On Thursday, 30 January 2014 at 16:28:42 UTC, Namespace wrote:
> On Thursday, 30 January 2014 at 16:24:00 UTC, Stanislav Blinov wrote:
>>> void main() {
>>> 	F f;
>>> 	int i = f(3,4,5);
>>> 	float f_ = f!float(6, 7, 8);
>>> }
>>> ----
>>>
>>> Does not work, it fails with:
>>> Error: template instance f!float f is not a template declaration, it is a variable
>>
>> f.opCall!float(6, 7, 8);
>
> ... Yes, of course. But where is the sense to use opCall if I need to call it explicitly?

Could you not use opDispatch? Not sure if you can templatize it
or not though...
January 30, 2014
On Thursday, 30 January 2014 at 16:47:46 UTC, Frustrated wrote:
> On Thursday, 30 January 2014 at 16:28:42 UTC, Namespace wrote:
>> On Thursday, 30 January 2014 at 16:24:00 UTC, Stanislav Blinov wrote:
>>>> void main() {
>>>> 	F f;
>>>> 	int i = f(3,4,5);
>>>> 	float f_ = f!float(6, 7, 8);
>>>> }
>>>> ----
>>>>
>>>> Does not work, it fails with:
>>>> Error: template instance f!float f is not a template declaration, it is a variable
>>>
>>> f.opCall!float(6, 7, 8);
>>
>> ... Yes, of course. But where is the sense to use opCall if I need to call it explicitly?
>
> Could you not use opDispatch? Not sure if you can templatize it
> or not though...

Example? I did not know how.
January 30, 2014
On Thursday, 30 January 2014 at 15:59:28 UTC, Namespace wrote:
> Here: http://dlang.org/operatoroverloading.html#FunctionCall
> is this example:
> ----
> import std.stdio;
>
> struct F {
> 	int opCall() {
> 		return 0;
> 	}
> 	
> 	int opCall(int x, int y, int z) {
> 		return x * y * z;
> 	}
> }
>
> void main() {
> 	F f;
> 	int i;
>
> 	i = f();      // same as i = f.opCall();
> 	i = f(3,4,5); // same as i = f.opCall(3,4,5);
> }
> ----
>
> And it works of course. But what if I want to templatize opCall? How can I call it?
>
> ----
> import std.stdio;
>
> struct F {
> 	T opCall(T = int)(int a, int b, int c) {
> 		return cast(T)(a * b * c);
> 	}
> }
>
> void main() {
> 	F f;
> 	int i = f(3,4,5);
> 	float f_ = f!float(6, 7, 8);
> }
> ----
>
> Does not work, it fails with:
> Error: template instance f!float f is not a template declaration, it is a variable

This is probably a bug. You should file it in Bugzilla.
January 30, 2014
On Thursday, 30 January 2014 at 16:53:33 UTC, Namespace wrote:
> On Thursday, 30 January 2014 at 16:47:46 UTC, Frustrated wrote:
>> On Thursday, 30 January 2014 at 16:28:42 UTC, Namespace wrote:
>>> On Thursday, 30 January 2014 at 16:24:00 UTC, Stanislav Blinov wrote:
>>>>> void main() {
>>>>> 	F f;
>>>>> 	int i = f(3,4,5);
>>>>> 	float f_ = f!float(6, 7, 8);
>>>>> }
>>>>> ----
>>>>>
>>>>> Does not work, it fails with:
>>>>> Error: template instance f!float f is not a template declaration, it is a variable
>>>>
>>>> f.opCall!float(6, 7, 8);
>>>
>>> ... Yes, of course. But where is the sense to use opCall if I need to call it explicitly?
>>
>> Could you not use opDispatch? Not sure if you can templatize it
>> or not though...
>
> Example? I did not know how.


doesn't seem to work with templates, I suppose you could try and
add it as a feature request?

module main;

import std.stdio;

interface A
{
	void foo();
	static final New() { }
}

class B : A
{
	void foo() { writeln("this is B.foo"); }

	void opDispatch(string s, T)(int i) {
		writefln("C.opDispatch('%s', %s)", s, i);
	}

}


void main() {
	B a = new B;
	//a.foo();

	a.test!int(3); // any *good* reason why this shouldn't work?

}
January 30, 2014
Also,

http://dlang.org/operatoroverloading.html#Dispatch

and possible solution to your problem:

http://www.digitalmars.com/d/archives/digitalmars/D/opDispatch_and_template_parameters_117095.html

Couldn't get code to compile though... but if it did, it should
solve your problem.

January 30, 2014
On Thursday, 30 January 2014 at 16:55:01 UTC, Meta wrote:
> On Thursday, 30 January 2014 at 15:59:28 UTC, Namespace wrote:
>> Here: http://dlang.org/operatoroverloading.html#FunctionCall
>> is this example:
>> ----
>> import std.stdio;
>>
>> struct F {
>> 	int opCall() {
>> 		return 0;
>> 	}
>> 	
>> 	int opCall(int x, int y, int z) {
>> 		return x * y * z;
>> 	}
>> }
>>
>> void main() {
>> 	F f;
>> 	int i;
>>
>> 	i = f();      // same as i = f.opCall();
>> 	i = f(3,4,5); // same as i = f.opCall(3,4,5);
>> }
>> ----
>>
>> And it works of course. But what if I want to templatize opCall? How can I call it?
>>
>> ----
>> import std.stdio;
>>
>> struct F {
>> 	T opCall(T = int)(int a, int b, int c) {
>> 		return cast(T)(a * b * c);
>> 	}
>> }
>>
>> void main() {
>> 	F f;
>> 	int i = f(3,4,5);
>> 	float f_ = f!float(6, 7, 8);
>> }
>> ----
>>
>> Does not work, it fails with:
>> Error: template instance f!float f is not a template declaration, it is a variable
>
> This is probably a bug. You should file it in Bugzilla.

Ok, done:
https://d.puremagic.com/issues/show_bug.cgi?id=12043
January 30, 2014
On Thursday, 30 January 2014 at 17:46:19 UTC, Frustrated wrote:
> Also,
>
> http://dlang.org/operatoroverloading.html#Dispatch
>
> and possible solution to your problem:
>
> http://www.digitalmars.com/d/archives/digitalmars/D/opDispatch_and_template_parameters_117095.html
>
> Couldn't get code to compile though... but if it did, it should
> solve your problem.

opDispatch is called if you try to call a non existing member. But in this case you don't call a member. ;)
« First   ‹ Prev
1 2 3