New answers tagged

5

It somewhat unlikely but possible that someone has built code that looks for this specific error and handles it in some special way that is different from what it would do if there were no results. Maybe they might return an error to the user. Such an approach is bad-practice IMO but I've seen (much) worse. I think that it's a mistake that this syntax ...


2

I would go for the clone method if the class involved was polymorphic (so that an instance of the right runtime type was created). Otherwise it's a matter of taste in my opinion.


3

Use copy constructors. Here's why: IClonable semantics are ambiguous. Microsoft never specified whether a clone should be a shallow or deep copy. You can specify custom behavior in your copy constructor, such as giving each copy its own unique ID or only copying some fields and not others. Further Reading Copy constructor vs Clone in C#


0

With respect to modifying records when the user presses "ok" in the dialog, you must compare it with the database record instance. You cannot rely on cloning the original HTTP get because someone may have modified the record before you and you get race conditions. If this is not the case, this has been covered by another thread and I am hoping it helps you ...


1

If a compiler behaves like this, it isn't an Ada compiler. My guess is that you use GCC (GNAT), which for some odd reason requires that you pass it a very specific combination of command line arguments, to behave like an Ada compiler: -fstack-check -- Stack overflow -gnato -- Arithmetic overflow -gnat2012 -- Use Ada 2012 (instead of older ...


4

Not all Ada compilers will behave like this. You must be using a version of the GNAT GPL compiler prior to 2015, or FSF GCC prior to 5; at that point, GNAT didn’t check for integer overflow by default - you had to compile with -gnato. This was stated to be in the pursuit of efficiency, but many users thought that the gain in efficiency wasn’t worth the cost ...


0

Because implementations of the language are likely to use primitive types as value types rather than reference types. That is, when you assign a value to a new variable, rather than changing the variable such that a contains a reference to the same object used in the source expression, it might copy the value of that object into the variable (if you know C#,...


0

First, I'll repeat that a modulo b should be equal to a - b * (a div b), and if a language doesn't provide that, you are in an awful mathematical mess. That expression a - b * (a div b) is actually how many implementations calculate a modulo b. There are some possible rationales. The first is that you want maximum speed, so a div b is defined as whatever ...


1

On the one hand we expect convenience, on the other strict OOP principles being applied. If you implement too many convenient overloads, you will soon be implementing a universal type converter in a string class. And then something else. I guess keeping things mean and lean and sticking to the single responsibility principle outweighed the need for more ...


1

This constructor takes an array of chars as its input. For your use case, you can simply: var bar = new string(foo.Select(x => x).ToArray()); If you wanted a rationale, you could say that the lazy-loading behavior of the IEnumerable does not give you any added benefit here since you need to read the whole thing anyway to create the string. Whether ...


4

Since there is a constructor overload that takes an array of characters, this should work: var bar = new string(foo.Select(x => x).ToArray()); Which pretty much eliminates the need for another constructor overload, as the proposed overload would essentially have to do the same thing. Eric Lippert often discusses why certain features don't make it ...


1

To answer your actual question: Most higher level (or scripting) languages out there have data structures that can hold different types of data (like numbers, strings and even functions) in the same structure ... So how are these types of data structures found in higher level languages implemented in the lower level language they are written in? You ...


7

The closest thing I managed to write was a class in C++ which allocated a certain amount of memory and, when its size was filled, allocated a longer block of memory, copied the elements in that block and freed the first one, but that doesn't seem efficient at all and anyway only allows to add one specific type of item and only in a stack-like way (where you ...


1

Your quote is still true today: Dealing with large numbers of interrelated types while still preserving modularity in the design of large systems is very difficult, and is an area of much current research. Programming very large object oriented systems -- thus with more and more interrelated classes and dependencies -- caused mainstream language ...


0

Mixed types are accommodated by storing pointers to objects. The closest thing I managed to write was a class in C++ which allocated a certain amount of memory and, when its size was filled, allocated a longer block of memory, copied the elements in that block and freed the first one, but that doesn't seem efficient at all It's quite efficient in ...


8

I guess the kind of "lists" you had in mind are not the "linked lists" mentioned by Robert Harvey, but the kind of arrays which are called list in Python, List or ArrayList in C#, ArrayList in Java, or std::vector in C++. Another popular term for this kind of data structure is "dynamic array". These are indeed implemented internally exactly the way you ...


4

Arrays, like all data structures, trade one kind of efficiency for another. The efficiency that an array specializes in is that of rapidly looking up an element by its index. It does that, not by searching through the array for the correct element, but by performing a mathematical calculation. If an array has elements of size 8 bytes, and you want to look ...


7

Let's be honest - nobody* is going to use your language. You're not going to get it done, or it's not going to work, or it's going to be too slow, or it's not going to be useful, or you're not going to be able to market it, or people are going to look at it and go... "meh, I'll just keep using X". So is it important to you that the language be self-...


3

There are several programming languages that successfully combine functional and imperative paradigms, such as C# and Scala. Lisp even has a loop macro, and it incorporates Lisp-like syntax (more or less): (loop for x in '(a b c d e) do (print x) ) (loop for x from 1 to 5 for y = (* x 2) collect y) Note that Lisp loops are implemented ...


0

This could be a good strategy for a search algorithm. Supose you have recursive algorithm that performs very deep searches. When you found the element that satisfies your search criteria you can just throw an "OnFoundEvent". If you found nothing, you can throw an "OnNotFoundEvent". With this technique you skip all the backtracking mechanism caused by the ...


5

There is an additional case which wasn't mentioned in the previous answers: mocks for unit tests. A mock can need to implement only a small part of an interface, but to compile, it should declare all of them. The not implemented exception makes then a very clear difference between methods which are actually required by the test, but return nothing or a ...


11

It allows the code to compile for your method stub (regardless of the method's return type), while you get around to putting in an implementation. It also reminds you to put in the implementation, because it will throw the first time you try to MouseDown on that textbox. A thrown exception that says "This method is not implemented" is much better than ...



Top 50 recent answers are included