www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Casting functions to delegates

reply Matthias Walter <xammy xammy.homelinux.net> writes:
Hi all,

there was a discussion in 2006 but w/o a result, and I didn't find any
bugs about implicitely casting functions to delegates. It seems to be
impossible as long as function-pointer calls work differently than
delegate calls in the ABI. Will this at some point be fixed?

If not, is there a conversion function in phobos (I didn't find one)? I
played a bit and found that this one might work:

R delegate(A) funcionToDelegate (R, A...) (R function(A) func)
{
  return (A a){ return func(a); };
}

Matthias
Dec 11 2010
next sibling parent "Craig Black" <craigblack2 cox.net> writes:
Not sure if this is the de facto answer, but I found this one from back in 
the day.

R delegate(T) toDg(R, T...)(R function(T) fp) {
    struct dg {
        R opCall(T t) {
            return (cast(R function(T)) this) (t);
        }
    }
    R delegate(T) t;
    t.ptr = fp;
    t.funcptr = &dg.opCall;
    return t;
 }

-Craig 
Dec 11 2010
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 12.12.2010 7:25, Matthias Walter wrote:
 Hi all,

 there was a discussion in 2006 but w/o a result, and I didn't find any
 bugs about implicitely casting functions to delegates. It seems to be
 impossible as long as function-pointer calls work differently than
 delegate calls in the ABI. Will this at some point be fixed?

 If not, is there a conversion function in phobos (I didn't find one)? I
 played a bit and found that this one might work:

 R delegate(A) funcionToDelegate (R, A...) (R function(A) func)
 {
    return (A a){ return func(a); };
 }

 Matthias

toDelegate, which does it in the most general way, functional objects covered. Sadly enough it's not listed in docs, because of the auto return. -- Dmitry Olshansky
Dec 12 2010
prev sibling parent Matthias Walter <xammy xammy.homelinux.net> writes:
On 12/12/2010 06:15 AM, Dmitry Olshansky wrote:
 On 12.12.2010 7:25, Matthias Walter wrote:
 Hi all,

 there was a discussion in 2006 but w/o a result, and I didn't find any
 bugs about implicitely casting functions to delegates. It seems to be
 impossible as long as function-pointer calls work differently than
 delegate calls in the ABI. Will this at some point be fixed?

 If not, is there a conversion function in phobos (I didn't find one)? I
 played a bit and found that this one might work:

 R delegate(A) funcionToDelegate (R, A...) (R function(A) func)
 {
    return (A a){ return func(a); };
 }

 Matthias

toDelegate, which does it in the most general way, functional objects covered. Sadly enough it's not listed in docs, because of the auto return.

Dec 12 2010