immutability

How do I create a Vec from a range and shuffle it?

给你一囗甜甜゛ 提交于 2019-12-01 13:42:28
问题 I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec<u32> = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following error: error[E0308]: mismatched types --> src/main.rs:9:26 | 9 | thread_rng().shuffle(slice); | ^^^^^ types differ in mutability | = note: expected type `&mut [_]` found type `&[u32]` I think I understand that the content of vectors and slices is immutable and that

How to declare a local constant in C#?

萝らか妹 提交于 2019-12-01 13:41:09
问题 How to declare a local constant in C# ? Like in Java, you can do the following : public void f(){ final int n = getNum(); // n declared constant } How to do the same in C# ? I tried with readonly and const but none seems to work. Any help would be greatly appreciated. Thanks. 回答1: In C#, you cannot create a constant that is retrieved from a method. Edit: dead link http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.71).aspx This doc should help: https://docs.microsoft.com/en-us/dotnet/csharp

Moving objects across AppDomains in .NET

与世无争的帅哥 提交于 2019-12-01 12:32:08
Is there a way to efficiently share or move .NET objects across AppDomains? I realize that the intent of AppDomains is to provide isolation - however I have a case where I need to move a relatively large, cached set of immutable objects that are expensive to compute and create. At the moment, I have a serialization approach that works, but is rather slow. You cannot move an object across an AppDomain without serializing it. That is the main point of an AppDomain - you can almost think of it as a completely separate process. This is where MarshallByRefObject comes into play. It allows you to

F# Immutable variable sized window data structure

℡╲_俬逩灬. 提交于 2019-12-01 09:13:53
I have below a description of a data structure I need and I want to implement it using immutable data structures. I'm trying to determine... is there an existing data structure out there that will support what I'm trying to do here or do I need to create one--and if I need to create it, what would be a good place to start (building blocks)? I have a steady stream of incoming values of a certain type. I want to add them to a persistent/immutable data structure to hold a history of them, and on each add, it will review the history and determine if one or more oldest items will be removed (for

What is the purpose of modifying a string using reflection?

爱⌒轻易说出口 提交于 2019-12-01 08:39:49
I was reading an article that said that Java strings are not completely immutable. However, in the article's sample code that modifies the string, it makes a call to string.toUpperCase().toCharArray(), which returns a new string. So what's the purpose of going through the process of changing the string if you call toUpperCase() anyway? Here is the code: public static void toUpperCase(String orig) { try { Field stringValue = String.class.getDeclaredField("value"); stringValue.setAccessible(true); stringValue.set(orig, orig.toUpperCase().toCharArray()); } catch (Exception ex){} } Also, I noticed

Why do most classes of the HttpClient API define immutable objects?

我的未来我决定 提交于 2019-12-01 08:05:12
The documentation for HttpClient states the following about immutability: Interceptors exist to examine and mutate outgoing requests and incoming responses. However, it may be surprising to learn that the HttpRequest and HttpResponse classes are largely immutable. This is for a reason: because the app may retry requests, the interceptor chain may process an individual request multiple times. If requests were mutable, a retried request would be different than the original request. Immutability ensures the interceptors see the same request for each try. I find it hard to understand this

can placement “new” be used to alter “const” data?

橙三吉。 提交于 2019-12-01 07:47:09
问题 [ This is a follow-up to can memcpy() be used to change “const” member data?. And Idiomatic Way to declare C++ Immutable Classes really gets at the issue, especially this answer "In a language designed around immutable data, it would know it can "move" your data despite its (logical) immutability." ] Given a struct with const members struct point2d { const int x; const int y; }; // can't change to remove "const" A class which holds a pointer to point2d can point to a new point2d instance with

F# Immutable variable sized window data structure

半世苍凉 提交于 2019-12-01 07:21:43
问题 I have below a description of a data structure I need and I want to implement it using immutable data structures. I'm trying to determine... is there an existing data structure out there that will support what I'm trying to do here or do I need to create one--and if I need to create it, what would be a good place to start (building blocks)? I have a steady stream of incoming values of a certain type. I want to add them to a persistent/immutable data structure to hold a history of them, and on

WebSQL: Are returned rows in SQLResultSetRowList immutable?

冷暖自知 提交于 2019-12-01 06:46:04
I've been fetching rows from a WebSQL DB, and the returned rows seems to be readonly. db.readTransaction( function(t1) { t1.executeSql( "SELECT * FROM Foo WHERE id = ?", [1], function(t2, resultSet){ var foo = resultSet.rows.item(0); console.log("before: " + foo.col1); foo.col1 = "new value"; console.log("after: " + foo.col1); console.log("sealed? " + Object.isSealed(foo)); console.log("frozen? " + Object.isFrozen(foo)); } ); } ); It prints: before: old value after: old value sealed? false frozen? false I had to manually clone the row to be able to modify it. How is this possible? How can an

Cheapest way of establishing happens-before with non-final field

眉间皱痕 提交于 2019-12-01 06:44:13
Many questions/answers have indicated that if a class object has a final field and no reference to it is exposed to any other thread during construction, then all threads are guaranteed to see the value written to the field once the constructor completes. They have also indicated that storing into a final field a reference to a mutable object which has never been accessed by outside threads will ensure that all mutations which have been made to the object prior to the store will be visible on all threads which access the object via the field. Unfortunately, neither guarantee applies to writes