mutable

Logical const in D

半城伤御伤魂 提交于 2019-11-28 00:49:00
D has two types of constness: immutable variables are ones that were declared immutable, and always will be immutable, while const variables are simply read only versions of an object. Logical const is when a function is marked as const , but allows write access to one or more member variables. The typical use of this is for lazy evaluation, e.g. (in C++) struct Matrix { double determinant() const { if ( m_dirty ) { m_determinant = /* expensive calculation */; m_dirty = false; } return m_determinant; } void set(int i, int j, double x) { m_dirty = true; ...; } mutable bool m_dirty; mutable

Swift - How to mutate a struct object when iterating over it

情到浓时终转凉″ 提交于 2019-11-27 23:02:28
问题 I am still not sure about the rules of struct copy or reference. I want to mutate a struct object while iterating on it from an array: For instance in this case I would like to change the background color but the compiler is yelling at me struct Options { var backgroundColor = UIColor.blackColor() } var arrayOfMyStruct = [MyStruct] ... for obj in arrayOfMyStruct { obj.backgroundColor = UIColor.redColor() // ! get an error } 回答1: struct are value types, thus in the for loop you are dealing

Does Java have mutable types for Integer, Float, Double, Long?

不打扰是莪最后的温柔 提交于 2019-11-27 21:45:48
I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in? http://www.java2s.com/Code/Java/Data-Type/Amutableintwrapper.htm Bozho No, Java doesn't have these built in. And that is for a reason. Using mutable types is dangerous, as they can easily be misused. Additionally, it is really easy to implement it. For example, commons-lang has a MutableInt . You could always wrap the value in an array like int[] mutable = {1}; if including the code for a mutable wrapper class is too cumbersome. Since JDK

Mutable strings in Python

◇◆丶佛笑我妖孽 提交于 2019-11-27 20:25:52
Please, do you know of a Python library which provides mutable strings? Google returned surprisingly few results. The only usable library I found is http://code.google.com/p/gapbuffer/ which is in C but I would prefer it to be written in pure Python. Edit: Thanks for the responses but I'm after an efficient library. That is, ''.join(list) might work but I was hoping for something more optimized. Also, it has to support the usual stuff regular strings do, like regex and unicode. Jason Morgan In Python mutable sequence type is bytearray see this link This will allow you to efficiently change

Loading a resource to a mutable bitmap

浪尽此生 提交于 2019-11-27 20:05:42
I am loading a bitmap from a resource like so: Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image); What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would seem wasteful to repeat lots of drawing in my main loop when it isn't going to change). I am making the changes to the bitmap with the following: Canvas c = new Canvas(mBackground); c.drawARGB(...); // etc So naturally I get an exception java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor So to avoid that I made a copy of the

Mutable variable is accessible from closure. How can I fix this?

坚强是说给别人听的谎言 提交于 2019-11-27 19:45:27
问题 I am using Typeahead by twitter. I am running into this warning from Intellij. This is causing the "window.location.href" for each link to be the last item in my list of items. How can I fix my code? Below is my code: AutoSuggest.prototype.config = function () { var me = this; var comp, options; var gotoUrl = "/{0}/{1}"; var imgurl = '<img src="/icon/{0}.gif"/>'; var target; for (var i = 0; i < me.targets.length; i++) { target = me.targets[i]; if ($("#" + target.inputId).length != 0) {

Using volatile keyword with mutable object

旧时模样 提交于 2019-11-27 19:40:49
In Java, I understand that volatile keyword provides visibility to variables. The question is, if a variable is a reference to a mutable object, does volatile also provide visibility to the members inside that object? In the example below, does it work correctly if multiple threads are accessing volatile Mutable m and changing the value ? example class Mutable { private int value; public int get() { return a; } public int set(int value) { this.value = value; } } class Test { public volatile Mutable m; } This is sort of a side note explanation on some of the details of volatile. Writing this

How are mutable arrays implemented in Haskell?

◇◆丶佛笑我妖孽 提交于 2019-11-27 19:21:22
I've read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the "type" Array itself should be defined, they only gave definitions for the functions using monads to access or modify this type. How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell ?! (such as STUArray and MArray) How are arrays, having O(1) time to access or modify an indexed element, implemented in Haskell They are implemented via primitive operations in the runtime system for

Why did Matz choose to make Strings mutable by default in Ruby?

丶灬走出姿态 提交于 2019-11-27 19:15:56
It's the reverse of this question: Why can't strings be mutable in Java and .NET? Was this choice made in Ruby only because operations (appends and such) are efficient on mutable strings, or was there some other reason? (If it's only efficiency, that would seem peculiar, since the design of Ruby seems otherwise to not put a high premium on faciliating efficient implementation.) This is in line with Ruby's design, as you note. Immutable strings are more efficient than mutable strings - less copying, as strings are re-used - but make work harder for the programmer. It is intuitive to see strings

Cocoa: Testing to find if an NSString is immutable or mutable?

这一生的挚爱 提交于 2019-11-27 18:21:44
问题 This produces an immutable string object: NSString* myStringA = @"A"; //CORRECTED FROM: NSMutableString* myStringA = @"A"; This produces a mutable string object: NSMutableString* myStringB = [NSMutableString stringWithString:@"B"]; But both objects are reported as the same kind of object, "NSCFString": NSLog(@"myStringA is type: %@, myStringB is type: %@", [myStringA class], [myStringB class]); So what is distinguishing these objects internally, and how do I test for that, so that I can