language-implementation

How is asynchronous callback implemented?

限于喜欢 提交于 2019-12-07 00:19:54
问题 How do all the languages implements asynchronous callbacks? For example in C++, one need to have a "monitor thread" to start a std::async . If it is started in main thread, it has to wait for the callback. std::thread t{[]{std::async(callback_function).get();}}.detach(); v.s. std::async(callback_function).get(); //Main thread will have to wait What about asynchronous callbacks in JavaScript? In JS callbacks are massively used... How does V8 implement them? Does V8 create a lot of threads to

Private inner class synthesizes unexpected anonymous class

前提是你 提交于 2019-12-06 01:58:06
问题 When you compile a Java class with a private inner class, it appears that an anonymous class is automatically synthesized along with it for some reason. This class is sufficient to reproduce it: public class SynthesizeAnonymous { public static void method() { new InnerClass(); } private static class InnerClass {} } When compiled, this generates the expected SynthesizeAnonymous.class and SynthesizeAnonymous$InnerClass.class files, but it also generates a strange SynthesizeAnonymous$1.class

Lua: understanding table array part and hash part

不羁岁月 提交于 2019-12-05 14:55:18
In section 4, Tables, in The Implementation of Lua 5.0 there is and example: local t = {100, 200, 300, x = 9.3} So we have t[4] == nil . If I write t[0] = 0 , this will go to hash part . If I write t[5] = 500 where it will go? Array part or hash part ? I would eager to hear answer for Lua 5.1, Lua 5.2 and LuaJIT 2 implementation if there is difference. Contiguous integer keys starting from 1 always go in the array part. Keys that are not positive integers always go in the hash part. Other than that, it is unspecified, so you cannot predict where t[5] will be stored according to the spec (and

How is “letrec” implemented without using “set!”?

ε祈祈猫儿з 提交于 2019-12-05 06:07:11
How can letrec be implemented without using set! ? It seems to me that set! is an imperative programming construct, and that by using it, one loses the benefits of functional programming. I know usually we ask content to be copied but there is no short answer to your question. http://www.cs.indiana.edu/~dyb/pubs/fixing-letrec.pdf No. Just because a functional feature is implemented with imperative code behind the scenes, that doesn't make the feature be imperative. Our computing machines are all imperative; so at some point all functional code must be implemented by translation into imperative

How is asynchronous callback implemented?

时间秒杀一切 提交于 2019-12-05 05:43:46
How do all the languages implements asynchronous callbacks? For example in C++, one need to have a "monitor thread" to start a std::async . If it is started in main thread, it has to wait for the callback. std::thread t{[]{std::async(callback_function).get();}}.detach(); v.s. std::async(callback_function).get(); //Main thread will have to wait What about asynchronous callbacks in JavaScript? In JS callbacks are massively used... How does V8 implement them? Does V8 create a lot of threads to listen on them and execute callback when it gets message? Or does it use one thread to listen on all the

How are arrays implemented in Perl?

梦想与她 提交于 2019-12-04 22:45:41
The Perl array is an abstract data type. What's the internal mechanism for the Perl array? Is it implemented with dynamic array or linked list? Since the array elements have random access, I would assume a dynamic array of pointers, or references to scalars make sense. However, with shift and unshift operation at the head of array, would the array have to move all its elements with these operations? sound inefficient to me. Any thought? Have a look at this: http://www.perlmonks.org/?node_id=17890 (taken from there:) Perl implements lists with an array and first/last element offsets. The array

Why is the main method entry point in most C# programs static?

回眸只為那壹抹淺笑 提交于 2019-12-04 22:13:32
Why is the main method entry point in most C# programs static? In order to call an instance method you need an instance of an object. This means in order to start your program the CLR would need to create an instance of say Program in order to call the method Main . Hence the constructor of Program would run before Main which defeats the purpose of having a main altogether. I'd turn the question around. What is the compelling benefit of implementing the feature that allows Main to be an instance method? Features are expensive; if there is no compelling benefit, they don't get implemented. Do

Access iterator object within a debugger

不打扰是莪最后的温柔 提交于 2019-12-04 06:56:23
问题 Is there really no way to access the iterator object or its state from inside a for-loop? Using pdb or ipdb for example, I can loop over and over again with n , but not see in which iteration I am (ex post, I mean; of course I could use enumerate, but only before starting the debugger). def creates an object, and for does the same, doesn't it? But the function has a name - and the iterator has not, is not accessible in memory? By the way, is the function accessible from within its body

Private inner class synthesizes unexpected anonymous class

一个人想着一个人 提交于 2019-12-04 05:29:31
When you compile a Java class with a private inner class, it appears that an anonymous class is automatically synthesized along with it for some reason. This class is sufficient to reproduce it: public class SynthesizeAnonymous { public static void method() { new InnerClass(); } private static class InnerClass {} } When compiled, this generates the expected SynthesizeAnonymous.class and SynthesizeAnonymous$InnerClass.class files, but it also generates a strange SynthesizeAnonymous$1.class file that corresponds to an anonymous subclass of java.lang.Object that was synthesized. If you look at

Python: the mechanism behind list comprehension

谁说我不能喝 提交于 2019-12-03 14:22:01
When using list comprehension or the in keyword in a for loop context, i.e: for o in X: do_something_with(o) or l=[o for o in X] How does the mechanism behind in works? Which functions\methods within X does it call? If X can comply to more than one method, what's the precedence? How to write an efficient X , so that list comprehension will be quick? The, afaik, complete and correct answer. for , both in for loops and list comprehensions, calls iter() on X . iter() will return an iterable if X either has an __iter__ method or a __getitem__ method. If it implements both, __iter__ is used. If it