www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do i use std.functional.binaryFun?

reply "Gary Willoughby" <dev nomad.so> writes:
How do i successfully use std.functional.binaryFun in the 
following example?

import std.stdio;
import std.functional;

class Foo(T, alias greater = "a > b") if 
(is(typeof(binaryFun!(greater)(T.init, T.init)) == bool))
{
	private alias compare = binaryFun!(greater);

	public this()
	{
		writefln("%s", this.compare(2, 1));
	}
}

void main(string[] args)
{
	auto foo = new Foo!(int, "a < b"); // Works

	auto bar = new Foo!(int, delegate(int a, int b){ return a > b; 
}); // Linker error.
}

The first instantiation works when using a string but I get a 
linker error when i try and use a delegate as the compare 
function. Why is this? and what do i need to do to correct this?
Dec 08 2014
parent reply "anonymous" <anonymous example.com> writes:
On Monday, 8 December 2014 at 20:08:35 UTC, Gary Willoughby wrote:
 import std.stdio;
 import std.functional;

 class Foo(T, alias greater = "a > b") if 
 (is(typeof(binaryFun!(greater)(T.init, T.init)) == bool))
 {
 	private alias compare = binaryFun!(greater);

 	public this()
 	{
 		writefln("%s", this.compare(2, 1));
 	}
 }

 void main(string[] args)
 {
 	auto foo = new Foo!(int, "a < b"); // Works

 	auto bar = new Foo!(int, delegate(int a, int b){ return a > b; 
 }); // Linker error.
 }
Looks like a compiler bug. A call without "this." works. When you insert a call without "this.", other calls with "this." work, too. A delegate with an implicit parameter type works: `Foo!(int, delegate(int a, /*!*/ b){ return a > b;})`.
Dec 08 2014
parent "Gary Willoughby" <dev nomad.so> writes:
On Tuesday, 9 December 2014 at 01:17:47 UTC, anonymous wrote:
 Looks like a compiler bug.
Filed: https://issues.dlang.org/show_bug.cgi?id=13843
Dec 09 2014