language-features

How to deal with the immutability of returned structs?

谁说我不能喝 提交于 2019-12-03 00:15:36
I'm writing a game that has a huge 2D array of "cells". A cell takes only 3 bytes. I also have a class called CellMap, which contains the 2D array as a private field, and provides access to it via a public indexer. Profiling showed that a performance problem is caused by garbage collection of too many Cell objects. So I decided to make Cell a struct (it was a class). But now code like this doesn't work: cellMap[x, y].Population++; I can think of many options, but I don't really like any of them. Make the array public , and write cellMap.Data[x, y].Population = 5; Stop using a CellMap class ,

C++: Adding and redefinition of default arguments in real world

混江龙づ霸主 提交于 2019-12-02 20:47:37
There is a possibility to add or redefine default arguments of a function in C++. Let's look at the example: void foo(int a, int b, int c = -1) { std::cout << "foo(" << a << ", " << b << ", " << c << ")\n"; } int main() { foo(1, 2); // output: foo(1, 2, -1) // void foo(int a, int b = 0, int c); // error: does not use default from surrounding scope void foo(int a, int b, int c = 30); foo(1, 2); // output: foo(1, 2, 30) // void foo(int a, int b, int c = 35); // error: we cannot redefine the argument in the same scope // has a default argument for c from a previous declaration void foo(int a, int

Python: How to pass more than one argument to the property getter?

流过昼夜 提交于 2019-12-02 18:42:34
Consider the following example: class A: @property def x(self): return 5 So, of course calling the a = A(); a.x will return 5 But imagine that you want to be able to modify the property x. This way, for example: class A: @property def x(self, neg = False): return 5 if not neg else -5 And call it with a = A(); a.x(neg=True) That will raise a TypeError: 'int' object is not callable , that is quite normal, since our x is evaluated as 5 . So, I would like to know how one can pass more then one argument to the property getter, if it is possible at all. Note that you don't have to use property as a

Is there any way to make a function's return accessible via a property?

偶尔善良 提交于 2019-12-02 18:28:11
问题 I'm a JS dev, experimenting with functional programming ideas, and I'm wondering if there's anyway to use chains for synchronous functions in the way the promise chains are written. For example: function square (num) { return num * num; } let foo = 2 let a = square(foo) //=> 4 let b = square(square(foo)) //=> 16 Fair enough, but what I'd like to do (often to make code parsing easier) is to chain together those methods by passing that in as the first parameter of a chain. So that something

What features should Java 7 onwards have to encourage switching from C#? [closed]

走远了吗. 提交于 2019-12-02 17:33:51
C# has a good momentum at the moment. What are the features that you would need to have in order to switch (or return) to Java? It would also be quite useful if people posted workarounds for these for the current Java versions, e.g. Nullables being wrapped around custom classes, to make this a much more interesting wiki. No longer a user As a .NET/C# developer here are the missing features that annoy me. This list in no particular order - just as thoughts come to mind: The Java library is too small. For common things I have to choose between 5 competing open source products because the base

What unique features does Firebug have that are not built-in to Firefox?

橙三吉。 提交于 2019-12-02 14:05:43
I just cleaned my Firefox addons and wondered: Which features does Firebug have that make it unique? Which features are available in both Firebug and the Firefox Developer Tools? Sebastian Zartner Firefox's native developer tools have come a long way since this question was written. The differences have mainly reduced to the following points: Can't stop the script execution on DOM mutations, XHRs, or cookie changes. XPaths can't be copied. Missing an events side panel in the Inspector (though events are displayed within the DOM structure). Missing a DOM side panel in the Inspector. No live

Null-propagation replacement for null check prior conditional statement

旧巷老猫 提交于 2019-12-02 01:27:50
After seeing a similar question , I was wondering if the following expression ... if (attribute != null && attribute.Description == input) ... would behave (almost) identical, to following null-propagation variant? if (attribute?.Description == input) So far, I could determine only following (somehow minor) differences: not possible in case input is of non-nullable type in case input would be itself null , behavior would be altered Am I missing something? or are there other differences in behavior ? EDIT: in the end, the only fail-safe alternative I've found for the first snippet, would be: if

What is the prefered style for single decision and action statements?

最后都变了- 提交于 2019-12-01 22:07:54
In the case of languages that support single decision and action without brackets, such as the following example: if (var == true) doSomething(); What is the preferred way of writing this? Should brackets always be used, or should their usage be left as a preference of the individual developer? Additionally, does this practice depend on the size of the code block, such as in the following example: if (var == 1) doSomething(1); else if (var > 1 && var < 10) doSomething(2); else { validate(var); doSomething(var); } There isn't really a right answer. This is what coding standards within the

Is this the correct way of putting HTML in PHP?

社会主义新天地 提交于 2019-12-01 21:23:52
I'm new to PHP, and most of the time I have been 'echo'ing my HTML. Today, I found this way of doing it, which makes things 1000 times easier: <?php if(get_field('field_name')){ ?> <p>HTML can go here without echo'ing it</p> <?php } ?> But is it an accepted way? It seems to work every time. Thanks. Sampo Sarrala It's mostly about how you like to read your code. With IDE that has autocomplete/code suggestions it is best to use <?php if (...) { ?> so that autocompletion/suggestion features know when to use html and when php suggestions for your code. And of course it's nice to select coding

Why can't I use an array index on the return value of a function? [closed]

霸气de小男生 提交于 2019-12-01 18:54:17
Why can't I do this? explode(',','1,2,3', 1)[0] Every other language supports it. Answers I'm looking for: (since people seem to think this is a pointless rant) Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of? Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement? Why can't I do this? Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.: current(explode(',','1,2,3', 1)); Otherwise, you