immutability

What is immutability and why should I worry about it?

五迷三道 提交于 2019-11-26 23:54:24
问题 I've read a couple of articles on immutability but still don't follow the concept very well. I made a thread on here recently which mentioned immutability, but as this is a topic in itself, I am making a dedicated thread now. I mentioned in the past thread that I thought immutability is the process of making an object read only and giving it low visibility. Another member said it didn't really have anything to do with that. This page (part of a series) uses an example of an immutable class

Unexpected Behavior of Extend with a list in Python [duplicate]

假如想象 提交于 2019-11-26 23:27:50
问题 This question already has an answer here: Why does list.append evaluate to false in a boolean context? [duplicate] 7 answers I am trying to understand how extend works in Python and it is not quite doing what I would expect. For instance: >>> a = [1, 2, 3] >>> b = [4, 5, 6].extend(a) >>> b >>> But I would have expected: [4, 5, 6, 1, 2, 3] Why is that returning a None instead of extending the list? 回答1: The extend() method appends to the existing array and returns None . In your case, you are

Are Swift “mutable” strings really mutable, or are they just like Java strings?

自闭症网瘾萝莉.ら 提交于 2019-11-26 23:13:24
问题 In The Swift Programming Language , in the section on Strings, subsection String Mutability , it says this: You indicate whether a particular String can be modified (or mutated ) by assigning it to a variable (in which case it can be modified), or to a constant (in which case it cannot be modified): and gives example code: var variableString = "Horse" variableString += " and carriage" // variableString is now "Horse and carriage" let constantString = "Highlander" constantString += " and

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

别来无恙 提交于 2019-11-26 22:47:17
问题 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.) 回答1: This is in line with Ruby's design, as you note. Immutable strings are more efficient than mutable strings -

What does the comment “frozen_string_literal: true” do?

蓝咒 提交于 2019-11-26 22:30:37
问题 This is the rspec binstub in my project directory. #!/usr/bin/env ruby begin load File.expand_path("../spring", __FILE__) rescue LoadError end # frozen_string_literal: true # # This file was generated by Bundler. # # The application 'rspec' is installed as part of a gem, and # this file is here to facilitate running it. # require "pathname" ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", Pathname.new(__FILE__).realpath) require "rubygems" require "bundler/setup" load Gem.bin_path(

Why shouldn't I use immutable POJOs instead of JavaBeans?

十年热恋 提交于 2019-11-26 22:29:47
问题 I have implemented a few Java applications now, only desktop applications so far. I prefer to use immutable objects for passing the data around in the application instead of using objects with mutators (setters and getters ), also called JavaBeans. But in the Java world, it seems to be much more common to use JavaBeans, and I can't understand why I should use them instead. Personally the code looks better if it only deals with immutable objects instead of mutate the state all the time.

Does using public readonly fields for immutable structs work?

我们两清 提交于 2019-11-26 22:20:16
问题 Is this a proper way to declare immutable structs? public struct Pair { public readonly int x; public readonly int y; // Constructor and stuff } I can't think of why this would run into problems, but I just wanted to ask to make sure. In this example, I used ints. What if I used a class instead, but that class is also immutable, like so? That should work fine too, right? public struct Pair { public readonly (immutableClass) x; public readonly (immutableClass) y; // Constructor and stuff }

Why it's impossible to override `var` with `def` in Scala?

廉价感情. 提交于 2019-11-26 22:04:40
问题 While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? 回答1: That's related to the Liskov Substitution Principle: you can't assign weaker access privileges in subclass (even for Java).

PHP: immutable public member fields

前提是你 提交于 2019-11-26 21:47:47
问题 I need to create an immutable class which is simply a member field container. I want its fields to be instantiated once in its constructor (the values should be given as parameters to the constructor). I want the fields to be public but immutable. I could have done it with Java using the final keyword before each field. How is it done in PHP? 回答1: You should use __set and __get magic methods and declare that property as protected or private: /** * @property-read string $value */ class Example

Allen Holub wrote “You should never use get/set functions”, is he correct? [duplicate]

扶醉桌前 提交于 2019-11-26 21:40:46
This question already has an answer here: Why use getters and setters/accessors? [closed] 38 answers Allen Holub wrote the following, You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's instance variables (member fields that aren't constants), should always be private. Period. No exceptions. Ever. I mean it. (You can occasionally use protected methods