Skip to content

Commit

Permalink
fix Issue 4545 - Alias to members possible without 'this' instance
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jan 24, 2012
1 parent 86ecdab commit eedb994
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion type.dd
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ $(SECTION2 <a name="delegates">Delegates</a>,
$(P There are no pointers-to-members in D, but a more useful
concept called $(I delegates) are supported.
Delegates are an aggregate of two pieces of data: an
object reference and a function pointer. The object reference
object reference and a pointer to a non-static member function,
or a pointer to a closure and a pointer to a nested function.
The object reference
forms the $(I this) pointer when the function is called.
)

Expand Down Expand Up @@ -416,6 +418,21 @@ dg = &o.member; // dg is a delegate to object $(I o) and
fp(3); // call func(3)
dg(3); // call o.member(3)
-------------------

$(P The equivalent of member function pointers can be constructed
using anonymous lambda functions:)

---
class C {
int a;
int foo(int i) { return i + a; }
}

// mfp is the member function pointer
auto mfp = function(C self, int i) { return self.foo(i); };
auto c = new C(); // create an instance of C
mfp(c, 1); // and call c.foo(1)
---
)

$(SECTION2 $(D size_t) And $(D ptrdiff_t),
Expand Down

3 comments on commit eedb994

@denis-sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will new lambda syntax be used in documentation?
auto mfp = function(C self, int i) => self.foo(i);
is a bit shorter than
auto mfp = function(C self, int i) { return self.foo(i); };

Looks like
auto mfp = (C self, int i) => self.foo(i);
can't be used because it doesn't say is it a function or a delegate. We can't use these keywords instead of auto so the shortest clear way
function mfp = (C self, int i) => self.foo(i);
is invalid now.

@andralex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yah, we should migrate docs to the new syntax.

@denis-sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope it will not replace string-lambda template syntax where the old one behaves better:
map!"a * a"
look batter than
map!(a => a * a)
By the way, filled Issue 7357 for the syntax proposed in my previous message.

Please sign in to comment.