D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2256 - Function pointers do not implicitly convert to/from immutable
Summary: Function pointers do not implicitly convert to/from immutable
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on: 3797
Blocks:
  Show dependency treegraph
 
Reported: 2008-07-30 22:45 UTC by sa
Modified: 2015-06-09 01:19 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description sa 2008-07-30 22:45:12 UTC
$ dmd -c memberfunptr.d 
memberfunptr.d(9): Error: cannot implicitly convert expression (__funcliteral1) of type void function(A) to invariant(void function(A))

$ cat memberfunptr.d 
==========================================
import std.stdio;

class A {
  void f() {writefln("f()");}
  void g() {writefln("g()");}
}
alias void function(A) FP;

invariant FP mfp1 = function void(A obj) {obj.f();};  // why this doesn't work?

// the following compiles, but ugly
static invariant FP mfp2;
static this() {
mfp2 = cast(invariant FP)(function void(A obj) {obj.g();});
}
==========================================

http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=74330

Why a function literal is not a constant expression?

Especially after I defining a global top level wrapper function in this case.
Under the hood, it should just be a raw pointer to some memory address, why this
cannot be a constant?
Comment 1 yebblies 2011-06-10 06:07:53 UTC
The 'is not a constant expression' part is covered by bug 2634.

https://github.com/D-Programming-Language/dmd/pull/96
fixes the implicit conversion to immutable.
Comment 2 yebblies 2011-09-02 06:09:35 UTC
(In reply to comment #1)
> The 'is not a constant expression' part is covered by bug 2634.
> 
> https://github.com/D-Programming-Language/dmd/pull/96
> fixes the implicit conversion to immutable.

https://github.com/D-Programming-Language/dmd/commit/306df8eaa6f8a987f76f401a1e03d8edf1f1e2ae

Now works as bug 3797 and bug 2634 have been fixed.