www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static function as member delegate's initial value?

reply OpenJelly <uyuiu DJ.CIM> writes:
I've got a delegate as a member of a class and I want to give it 
a default value. I can assign it an initial value in each 
initializer of the class but I'd like to make my code more 
readable and assign it a function at the declaration.

Trying to toDelegate the situation gives me the following Error:

Error: dummyDel.funcptr cannot be evaluated at compile time		

Is there a workaround? Or am I stuck assigning my default 
function to the delegate in each initializer?
Jun 20 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 20 June 2016 at 13:20:04 UTC, OpenJelly wrote:
 I've got a delegate as a member of a class and I want to give 
 it a default value. I can assign it an initial value in each 
 initializer of the class but I'd like to make my code more 
 readable and assign it a function at the declaration.

 Trying to toDelegate the situation gives me the following Error:

 Error: dummyDel.funcptr cannot be evaluated at compile time		

 Is there a workaround? Or am I stuck assigning my default 
 function to the delegate in each initializer?
For an instance memember, no you can't do that. Delegates are not compile time values and you can only include compile time values in declarations at module scope and class member scope. The best you can do is assign it in a constructor.
Jun 20 2016
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 20 June 2016 at 13:54:22 UTC, Mike Parker wrote:

 The best you can do is assign it in a constructor.
Well, 'initialize' it in a constructor.
Jun 20 2016