Video yükleniyor...

Video Yüklenemedi

Ana Sayfaya Dön

My thoughts on Closures and Lambdas

37,014 görüntüleme • 1 yıl önce •via X (Twitter)

5 Yorum

Dmitry /Undefined Behavior/ Sviridkin profil fotoğrafı
Dmitry /Undefined Behavior/ Sviridkin1 yıl önce

There's another argument in favour of using a fn pointer + context parameter rather than Rust closure: Rust closure cannot safely return reference to the captured data. With a fn pointer you can do whatever you want

Sasha Krassovsky profil fotoğrafı
Sasha Krassovsky1 yıl önce

I think usually the captures are heap-allocated (but with small object optimization), so a big reason to prefer not allowing closures is to avoid hidden heap allocations.

kantmissevershot profil fotoğrafı
kantmissevershot1 yıl önce

The benefit to actual closures (and also to directly passing around function items) is that closures don't have to pass around the actual function pointer. This is only a small performance benefit but it's worth talking about.

Adam Rezich profil fotoğrafı
Adam Rezich1 yıl önce

#import "Basic"; //Jai foo :: (f: (int, T) -> int, arg: $T) -> int { return f(69, arg); } main :: () { y := 2; print("%\n", foo((x, y) => x*y, y)); //lambda z := "hi"; print("%\n", foo((x: int, s: string) -> int { //anon print("%\n", s); return x*x; }, z)); }

İsmail Keskin profil fotoğrafı
İsmail Keskin1 yıl önce

Closures are also used for capturing things that will outlive their normal lifetime and in the most generic sense will require heap allocation if compiler can't prove lifetimes statically.

Benzer Videolar