immutability

How to confirm( or get the object repesentation of string) if string created in pool or not?

点点圈 提交于 2019-12-13 00:47:36
问题 I want to confirm if two String variable created is pointing to the same memory.The way I did it with normal class Ideone a=new Ideone(); Ideone b=a; System.out.println(a+" "+b); Output Ideone@106d69c Ideone@106d69c The output produced here is not the exact memory address but it gave hex code which are same and by which I can say that they both are pointing to same memory address or values. But in case of String String a="helloworld"; String b="hello"; String c=b+"world"; System.out.println(a

Converting Mutable to Immutable with Hibernate

邮差的信 提交于 2019-12-13 00:17:02
问题 I'm using Grails and I have the following domain class: class Pack { String code Date publishedDate //several other properties (including collections hasMany).... def isPublished() { return publishedDate != null } def publish() { publishedDate = new Date() } def canEdit() { return !isPublished() } } To sell a Pack I first need publish it and I use the publish method to publicate a Pack. After published, a Pack cannot be changed (i.e. after published, a Pack need to be a immutable instance).

Multiple constructors on an immutable (data) class

和自甴很熟 提交于 2019-12-12 21:14:47
问题 I'm trying to implement an immutable data class with more than one constructor. I felt that something like this should be possible: data class Color(val r: Int, val g: Int, val b: Int) { constructor(hex: String) { assert(Regex("#[a-fA-F0-6]{6}").matches(hex), { "$hex is not a hex color" } ) val r = hex.substring(1..2).toInt(16) val g = hex.substring(3..4).toInt(16) val b = hex.substring(5..6).toInt(16) this(r,g,b) } } Of course, it isn't: Kotlin expects the call to the main constructor be

Redux updating nested data [Immutable Update Patterns]

亡梦爱人 提交于 2019-12-12 17:50:17
问题 Can anyone help with this update pattern. I am not using any libraries like immer. I have to update a nested object and the data looks like dis Sample data { isFetching: false data:{ nba : { stack :{ 1:[] } } } } My Reducer { ...state, isFetching: false, data: { ...state.data, [action.payload.team]: { ...state[action.payload.team], [action.payload.framework]: { ...state[action.payload.framework], [action.payload.build]: action.payload.resp } } } }; I am able to update until second level but

Circular references with vals in Kotlin

吃可爱长大的小学妹 提交于 2019-12-12 17:24:44
问题 In Kotlin, say I have data class A (val f: B) and data class B (val f: A) . I want to initialize local var a: A and var b: B such that a.f is b and b.f is a . A.f and B.f must remain vals. Is this circular instantiation possible? data class A(val f: B) data class B(val f: A) fun foo() { var a: A var b: B // instantiate a and b here with a.f == b and b.f == a ?? } 回答1: Not exactly what you want but should work: interface A { val f: B } interface B { val f: A } data class AImpl(override var f:

How to make class immutable in python? [duplicate]

余生颓废 提交于 2019-12-12 14:14:45
问题 This question already has answers here : How to make an immutable object in Python? (24 answers) Closed 5 years ago . I have read a lot about this subject here but i still can't find an appropriate answer. I have a class like: class A(object): def __init__(self, first, second): self.first = first self.second = second def __eq__(self, other): return **** def __str__(self): return ***** def __repr__(self): return **** a = A("a", "b") How can i forbid a.first = "c" for example ? 回答1: You can

What is the difference between mutable values and immutable value redefinition?

痴心易碎 提交于 2019-12-12 11:55:10
问题 I have read that values in F# are immutable. However, I have also come across the concept of redefining value definitions, which shadow the previous ones. How is this different from a mutable value ? I ask this not just as a theoretical construct, but also if there is any advice on when to use mutable values and when to redefine expressions instead; or if someone can point out that the latter is not idiomatic f#. Basic example of redefinition: let a = 1;; a;; //1 let a = 2;; a;; //2 Update 1:

String Immutability

拜拜、爱过 提交于 2019-12-12 09:34:23
问题 Does string immutability work by statement, or by strings within a statement? For example, I understand that the following code will allocate two strings on the heap. string s = "hello "; s += "world!"; "hello" will remain on the heap until garbage collected; and s now references "hello world!" on the heap. However, how many strings does the following line allocate on the heap...1 or 2? Also, is there a tool/way to verify the results? string s = "goodbye " + "cruel world!"; 回答1: The compiler

Immutability in Hyperledger Fabric

蹲街弑〆低调 提交于 2019-12-12 09:24:19
问题 Can someone explain how the immutability is implemented in Hyperledger Fabric? If we have private channel with little amount of peers, how it can be guaranteed, that one side hasn't changed data in it's ledger? 回答1: In order to guarantee that no party in the channel has tampered data in its own favor you need to present sophisticated endorsement policy to include all required parties and make sure they adequately represented within endorsement policy. Hence making it obligatory for client

How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?

牧云@^-^@ 提交于 2019-12-12 08:19:29
问题 I'm trying to read a zip file, check that it has some required files, and then write all valid files out to another zip file. The basic introduction to java.util.zip has a lot of Java-isms and I'd love to make my code more Scala-native. Specifically, I'd like to avoid the use of vars . Here's what I have: val fos = new FileOutputStream("new.zip"); val zipOut = new ZipOutputStream(new BufferedOutputStream(fos)); while (zipIn.available == 1) { val entry = zipIn.getNextEntry if (entryIsValid