Slide 1

Slide 1 text

♯ ++

Slide 2

Slide 2 text

zobj = zipfile.open("some") zobj.read(name) # -> bytes def foo(cb: Callable[[str], bytes]) -> None: ...

Slide 3

Slide 3 text

• def • lambda • functools.partial • __call__

Slide 4

Slide 4 text

• __call__ • ZipFile • __call__ ZipFile • ZipFile.extract (name: str) -> None

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

• def • lambda • functools.partial • __call__ • myobj.read

Slide 7

Slide 7 text

• • ♯ • operator() •

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

• std::thread void • java.lang.Thread Runnable class Thread { Thread(Runnable target) {

Slide 10

Slide 10 text

• Runnable public interface Runnable { void run(); }

Slide 11

Slide 11 text

class MyRunnable implements Runnable { public void run() { System.out.println("MyRunnable running"); } } MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start();

Slide 12

Slide 12 text

Thread thread = new Thread(() -> { System.out.println("MyRunnable running"); }); thread.start();

Slide 13

Slide 13 text

• public interface Runnable { void run(); }

Slide 14

Slide 14 text

Thread thread = new Thread(() -> { System.out.println("MyRunnable running"); });

Slide 15

Slide 15 text

class lambda$1 implements Runnable { public void run() { System.out.println("MyRunnable running"); } } Thread thread = new Thread(new lambda$1());

Slide 16

Slide 16 text

__call__

Slide 17

Slide 17 text

public interface Function { R apply(T t); } class ZipFile { byte[] read(int) { } } public class Tab { static Tab take(Function g);

Slide 18

Slide 18 text

public interface Function { R apply(T t); } class ZipFile { byte[] read(int) { } } ZipFile zipped = new ZipFile("input.zip"); Tab::take(/* ?? */);

Slide 19

Slide 19 text

public interface Function { R apply(T t); } class ZipFile { byte[] read(int) { } } ZipFile zipped = new ZipFile("input.zip"); Tab::take(zipped::read);

Slide 20

Slide 20 text

class lambda$1 implements Function { final ZipFile arg0; byte[] apply(String t) { return arg0.read(t); } } ZipFile zipped = new ZipFile("input.zip"); Tab::take(new lambda$1(zipped));

Slide 21

Slide 21 text

• MyRunnable • lambda$1 • lambda$2

Slide 22

Slide 22 text

Slide 23

Slide 23 text

• void run() vptr … System.out.println("M yRunnable running");

Slide 24

Slide 24 text

• • void run() … System.out.println("M yRunnable running");

Slide 25

Slide 25 text

class Foo { public void test() { Thread thread = new Thread(() -> { System.out.println("MyRunnable running"); }); thread.start(); } }

Slide 26

Slide 26 text

class Foo { private static void lambda$test$0() { System.out.println("MyRunnable running"); } public void test() { Thread thread = new Thread( /* a callsite object to invoke lambda$test$0 */ ); thread.start(); } }

Slide 27

Slide 27 text

void run() … System.out.println("M yRunnable running");

Slide 28

Slide 28 text

• void run() System.out.println("M yRunnable running"); CallSite MethodHandle

Slide 29

Slide 29 text

Slide 30

Slide 30 text

♯ operator()

Slide 31

Slide 31 text

♯ • public interface Runnable { void run(); } • ♯ public delegate void Runnable();

Slide 32

Slide 32 text

♯ • public interface Runnable { void run(); } • ♯ public delegate void ThreadStart();

Slide 33

Slide 33 text

class MyRunnable implements Runnable { public void run() { System.out.println("MyRunnable running"); } } MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); thread.start();

Slide 34

Slide 34 text

class MyRunnable { public void run() { Console.WriteLine("MyRunnable running"); } } MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable.run); thread.Start();

Slide 35

Slide 35 text

myRunnable.run new ThreadStart(myRunnable.run)

Slide 36

Slide 36 text

new Lambda__1(myRunnable.run)

Slide 37

Slide 37 text

internal class Delegate__ThreadStart { public virtual void Invoke(); } internal class Lambda__1 : Delegate__ThreadStart { readonly MyRunnable arg0; public override void Invoke() { arg0.run(); } }

Slide 38

Slide 38 text

internal class Delegate__ThreadStart : System.Delegate { public virtual void Invoke(); } namespace System { public abstract partial class Delegate { // null if static delegate internal object? _target; // _methodPtr could point to a thunk internal IntPtr _methodPtr;

Slide 39

Slide 39 text

void run() System.out.println("M yRunnable running"); MethodHandle

Slide 40

Slide 40 text

myRunnable.run ♯ MyRunnable::run myRunnable void ThreadStart() _target _methodPtr

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

namespace System { public abstract partial class Delegate { // null if static delegate internal object? _target; // _methodPtr could point to a thunk internal IntPtr _methodPtr;

Slide 43

Slide 43 text

namespace std { template class function_ref { // null if free function void *_target = nullptr; // _methodPtr points to a thunk Ret (*_methodPtr)(void *, Params...);

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

pub trait Callable where Args: Tuple, { fn call(&self, args: Args); }

Slide 46

Slide 46 text

struct MyReportingCallback { name: String, // more fields } impl MyReportingCallback { // member functions go here }

Slide 47

Slide 47 text

impl MyReportingCallback { fn notify(&self, status: WorkResult, ref obj: Data) {} } fn accept>(cb: &T) { let w = WorkResult {}; let o = Data {}; cb.call((w, o)); }

Slide 48

Slide 48 text

impl MyReportingCallback { fn notify(&self, status: WorkResult, ref obj: Data) {} } impl Callable<(WorkResult, Data)> for MyReportingCallback { fn call(&self, (status, object): (WorkResult, Data)) { self.notify(status, object); } }

Slide 49

Slide 49 text

• trait Callable { fn call(&self, args: Args); } • template concept invocable = std::is_invocable_v;

Slide 50

Slide 50 text

• fn accept(cb: &T) where T: Callable<(WorkResult, Data)> • template T> void accept(T cb);

Slide 51

Slide 51 text

trait Callable { fn call(&self, args: Args); } impl Callable<(WorkResult, Data)> for MyReportingCallback { fn call(&self, (status, object): (WorkResult, Data)) { self.notify(status, object); } }

Slide 52

Slide 52 text

concept invocable { /*...*/ }; concept_map invocable { using operator() = MyReportingCallback::notify; };

Slide 53

Slide 53 text

pub trait FnOnce where Args: Tuple, { type Output; extern "rust-call" fn call_once(self, args: Args) -> Self::Output; } // std::thread pub fn spawn(f: F) -> JoinHandle where F: FnOnce() -> T

Slide 54

Slide 54 text

fn accept(cb: &T) where T: Callable<(WorkResult, Data)> fn accept(cb: &dyn Callable<(WorkResult, Data)>)

Slide 55

Slide 55 text

• impl • impl • MyReportingCallback IDoWorkCallback Callable<(WorkResult, Data)>

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

class CDraw : public IDraw { Color color_; public: void draw() override; void set_color(Color) override; ~CDraw() = default; }; class IDraw { public: virtual void draw() = 0; virtual void set_color(Color) = 0; ~IDraw() = default; };

Slide 58

Slide 58 text

vtable CDraw for IDraw void draw(); void set_color(Color); ~IDraw() vptr … Color color_;

Slide 59

Slide 59 text

constinit Drawable DrawableImpl = { .draw = [](void *this_) {}, .set_color = [](void *this_, Color color) {}, .destroy = [](void *this_) {}, }; struct Drawable { typedef void draw_t(void *); typedef void set_color_t(void *, Color); typedef void destroy_t(void *); draw_t *draw; set_color_t *set_color; destroy_t *destroy; };

Slide 60

Slide 60 text

DrawableImpl static object void (*draw)(void *); void (*set_color)(void *, Color); void (*destroy)(void *); ref-wrap CDraw rep_; ref-wrap void *obj_;

Slide 61

Slide 61 text

DrawableImpl static object void (*draw)(void *); void (*set_color)(void *, Color); void (*destroy)(void *); Drawable const & unique_ptr rep_; Drawable const & void *obj_;

Slide 62

Slide 62 text

Drawable const & unique_ptr rep_; Drawable const & void *obj_; Box &dyn Drawable

Slide 63

Slide 63 text

Drawable const & void *rep_; Drawable const & void *obj_; dyn Drawable dyn Drawable

Slide 64

Slide 64 text

constinit Drawable DrawableImpl = { .draw = [](void *this_) {}, .set_color = [](void *this_, Color color) {}, .destroy = [](void *this_) {}, }; struct Drawable { typedef void draw_t(void *); typedef void set_color_t(void *, Color); typedef void destroy_t(void *); draw_t *draw; set_color_t *set_color; destroy_t *destroy; };

Slide 65

Slide 65 text

impl Drawable for MyType { fn draw() { } fn set_color(color: Color) { } fn destroy() { } } trait Drawable { fn draw(); fn set_color(_: Color); fn destroy(); }

Slide 66

Slide 66 text

✓ ✓ ✓ Existential container Witness table

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

function_ref function_ref move_only_function move_only_function copyable_function copyable_function &dyn FnMut<(Args) -> R> &dyn Fn<(Args) -> R> Box R>> Box R>> Box R>> Box R>>

Slide 69

Slide 69 text

struct MyReportingCallback { void notify(WorkResult status, Data& object); }; void accept(std::function_ref cb);

Slide 70

Slide 70 text

MyReportingCallback cb; accept({std::nontype<&MyReportingCallback::notify>, cb});

Slide 71

Slide 71 text

MyReportingCallback cb; accept({std::nontype< [](auto &cb, WorkResult status, Data &object) { LOG(INFO) << "status: " << status; cb.notify(status, object); }>, cb});

Slide 72

Slide 72 text

concept_map invocable { using operator() = MyReportingCallback::notify; };

Slide 73

Slide 73 text

concept_map invocable { using operator() = MyReportingCallback::notify; };

Slide 74

Slide 74 text

constinit Drawable DrawableImpl = { .draw = [](void *this_) {}, .set_color = [](void *this_, Color color) {}, .destroy = [](void *this_) {}, };

Slide 75

Slide 75 text

constinit Drawable DrawableImpl = { .draw = [](void *this_) {}, .set_color = [](void *this_, Color color) {}, .destroy = [](void *this_) {}, };

Slide 76

Slide 76 text

MyReportingCallback cb; accept({std::nontype< [](auto &cb, WorkResult status, Data &object) { LOG(INFO) << "status: " << status; cb.notify(status, object); }>, cb});

Slide 77

Slide 77 text

template inline constexpr auto impl_invocable_for = std::nontype; template<> inline constexpr auto impl_invocable_for = std::nontype< [](auto &cb, WorkResult status, Data &object) { LOG(INFO) << "status: " << status; cb.notify(status, object); }>; MyReportingCallback cb; accept({impl_invocable_for, cb});

Slide 78

Slide 78 text

• () • + + () • ++

Slide 79

Slide 79 text

No content