-2

It is my understanding that in managed languages like C#/Java, anonymous functions are generated by the compiler at runtime and then JIT compiled into the program.

What about in unmanaged languages like Rust and C++? How can those languages create functions at runtime? What are the lower level details of implementing a lambda expression?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Tzeroxik
  • 29
  • 6
  • "*how do they generated code at runtime*" They don't, there is no JIT embedded within the exec. – Rakete1111 Apr 04 '18 at 13:21
  • Drawing parallels between the C++ and some managed language can prove to be counter productive. – Ron Apr 04 '18 at 13:22
  • 1
    C++ doesn't generate any code at runtime. It is a compiled language. – NathanOliver Apr 04 '18 at 13:22
  • 2
    This is just a `Syntactic sugar` for Java, C# and C++. Compiler creates a class instance or callback function at compile time, the only difference lambda is unnamed unlike callback created with another approach. And then this functional object used at run-time with the same way as normal. – Victor Gubin Apr 04 '18 at 13:25
  • maybe I didn't express myself well, I meant how are anonymous functions implemented in aot compiled languages? – Tzeroxik Apr 04 '18 at 13:25
  • 1
    That may differ from language to language. A google search for *how are lambdas implemented in * should answer your questions – NathanOliver Apr 04 '18 at 13:27
  • Oh I see, thanks Victor! – Tzeroxik Apr 04 '18 at 13:28
  • 4
    I sincerely doubt that Java or C# lambdas are actually JITted any more than named functions are. A lambda is still a compile-time construct, unlike (say) `eval`-style commands. – Angew is no longer proud of SO Apr 04 '18 at 13:28
  • So simplifying it's king of an object or a structure with a function pointer to it, whose variables/values will be allocated on either the stack or heap (depending on implementation) at runtime? – Tzeroxik Apr 04 '18 at 13:36
  • C# and Java can be AOT-compiled by .NET. Under .NET they are AOT-compiled languages. And Microsoft C++ can be used in a managed mode, it's a managed language (just like Java and C#). The question seems to carry some questionable assumptions. Anonymous != JIT. – ArtemGr Apr 04 '18 at 13:38
  • 1
    In Rust, the lambda is compiled (at compile time) as a method on struct containing captured variables. So there is no runtime. – Boiethios Apr 04 '18 at 13:48
  • I know that anonymous isn't the same as jit compiling, but in some places they refer that there's an anonymous class file created in java's lambda expressions that then is JIT compiled, and yes they are AOT compiled under the JVM/.Net/insertEnvHere, I might not have used the correct terms because english is not my native language, but you're not helping with all those assumptions in any way to answer this question, if I'm associating rust with c++ it's obvious that I'm not talking about managed c++. I welcome criticism to help me learn, but i'd appreciate some focus on the core question – Tzeroxik Apr 04 '18 at 13:48
  • - "_but in some places they refer that there's an anonymous class file created in java's lambda expressions that then is JIT compiled_" - yes, but **everything** is JIT-compiled there, not just the lambdas. - "_and yes they are AOT compiled under the JVM/.Net/insertEnvHere_" - no, Java is JIT on Oracle JVM, but can be AOT on .NET. – ArtemGr Apr 04 '18 at 16:03
  • As far as I know usually java is aot compiled to bytecode and the bytecode is the one being jit compiled on the jvm, yes it can be done for .net aot but why assume something that isn't standart, when someone says java usually it's assumed to run on the jvm, not .NET. – Tzeroxik Apr 04 '18 at 18:14
  • @Angew they actually are. Java lambdas do more lifting than just some static method or anonymous class. https://stackoverflow.com/a/30002627/1362755 – the8472 Apr 04 '18 at 18:19
  • @the8472 Thanks for the info, TIL something new. – Angew is no longer proud of SO Apr 04 '18 at 20:01

1 Answers1

2
int y = 3;
auto f = [y](int x) { return x*y; };

this is a C++11 lambda. The complier (basically) converts it to:

struct __anonymous_name__ {
  int operator()(int x) const { return x*y; }

  int y;
};
__anonymous_name__ f = {y};

where everything with __ in the name is not actually named, just given names for exposition purposes.

At runtime, everything has a fixed type, no code is generated.

std::function<int(int)> can store a copy of f above, but that uses a type erasure mechanism that goes beyond the scope of this question. Note, however, that f is not an object of type related to std::function<int(int)>; C++ has more than one kind of polymorphism.


I also seriously doubt that Java/C# lambdas are JIT'd any more than the rest of your code.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • AFAIK, in C# a lambda is converted to a class, just as in C++. And that's the same in Rust (struct + method). – Boiethios Apr 04 '18 at 13:50