New answers tagged

0

The concept of a "type" is really a human concept, over and above anything that corresponds to any physical implementation. When a programmer says that a variable is of type color, with values such as red, green, and blue, he is trying to describe a constraint that can be detected and therefore enforced by the programming language. He really ...


0

Read your example carefully. The loop for j doesn’t iterate log n times. It iterates zero times if n <= 0 and runs forever if n > 0. Once you figured out where you went wrong, you should be able to figure out the log n as well.


0

The easiest thing would be to write a webassembly compiler for your VM. If using your VM isn't necessary, you can use an embeddable webassembly runtime.


3

Big-O notation is about identifying the term that grows the fastest. It doesn't matter if the constant out the front is huge, or tiny. Its a constant and does not change how quickly a term grows. eg: 1/123456789 * N^3 + 123456789 * N^2 + 300000000000000000000 * N In the smaller values of N the linear term is dominant. But it is quickly over taken by the N^2 ...


2

You might want to look into refinement types, which are types with programmer-specified restrictions along the sort you mentioned. The main thing that determines if you don't need a runtime check is if you are casting from a subtype to a parent type. fibonacci is a subtype of int, for example, so a cast from fibonacci to int can be safely done without a ...


3

Yes, types are just constraints. Any general type system has top which means “this value can be anything” and bot (bottom) which is so constrained that no value can satisfy it. That’s literally chapter one of any type theory book. What can be determined at compile time depends on your operations. isEmail is trivial if you only allow constants in your ...


6

Yes, such masks and stencils will be performed efficiently. But at least when writing C, this is not automatic. In C and C++, every object must have an address. Bools are objects. The smallest addressable value is a char. So bools will generally be one byte large. While this matters for data structures (structs, arrays), it doesn't really matter for local ...


3

You decide which compilers you want to use or support. The available non-standard features follows directly from this choice of compilers. Some common software such as glibc, Linux, and GCC itself are primarily intended to be compiled with GCC, though Clang has good GCC compatibility. Similarly, you decide which C or C++ language standard you are targeting. ...


-2

Further continuing that thought ... Timer queues might be built using some kind of tree data structure, with the key value being the time-of-day that the event is supposed to run. (Trees simplify the process of inserting new values at any position needed.) The timer-runner examines the lowest-valued node of the tree to determine if the event should occur. ...


3

As Phillip's answer said, all the answers were basically up to how you design your execution model, there are various tradeoffs between different execution model. That said, if you're modeling your execution model similar to JavaScript, there are a couple things we do know with JavaScript. JavaScript uses an execution model that boils down to single threaded ...


6

For your custom programming language, the answer is "whatever you want it to be". Once you've defined your execution model, that will tell you what your call stacks look like. In particular here, you will need to decide whether your language has pre-emptive or cooperative concurrency. Unless you've already had those thoughts, you're jumping the gun ...


0

Many compiled languages including C++ do offer nice things like "reference-counted objects" and interpreter-style memory management. But, they necessarily require your discipline in order to use them properly. As long as you do that, they work great. (But, if you screw it up, they can't help you.) Interpreters, on the other hand, control their ...


5

It should be fairly algorithmic to know at compile time when an object is no longer needed. It is not. As Jörg notes in his answer, it is a provably undecidable problem in languages like C# and Java that allow for things like unsafe code, introspection, code generation, virtual dispatch, and user casting. For example, consider code where you pass a ...


1

But is that really something a compiler could not do? Yes. As with practically all questions of the form "couldn't the compiler know this", the answer is that no, it is not possible, because it is equivalent to Solving the Halting Problem.


Top 50 recent answers are included