Questions tagged [mutators]

Anything related to mutators (a.k.a. setters, or mutator methods) in object oriented programming, i.e. instance methods whose exclusive purpose is to change (part of) the internal state of an object to a specific value, without performing substantial additional processing.. This may also refer to mutator functions in non-OO languages when OOP techniques are used to emulate OOP-like encapsulation.

Overview

Anything related to mutators (a.k.a. setters, or mutator/setter methods) in Object Oriented Programming, i.e. instance methods whose exclusive purpose is to change (part of) the internal state of an object to a specific value, without performing substantial additional processing. This may also refer to mutator functions in non-OO languages when OOP techniques are used to emulate OOP-like encapsulation.

See also

217 questions
33
votes
3 answers

Appending turns my list to NoneType

In Python Shell, I entered: aList = ['a', 'b', 'c', 'd'] for i in aList: print(i) and got a b c d but when I tried: aList = ['a', 'b', 'c', 'd'] aList = aList.append('e') for i in aList: print(i) and got …
user460847
  • 1,518
  • 6
  • 25
  • 43
27
votes
3 answers

Python property descriptor design: why copy rather than mutate?

I was looking at how Python implements the property descriptor internally. According to the docs property() is implemented in terms of the descriptor protocol, reproducing it here for convenience: class Property(object): "Emulate…
nesdis
  • 1,065
  • 11
  • 14
17
votes
4 answers

Understanding struct-field mutation

From the Rust book about how to mutate struct fields: let mut point = Point { x: 0, y: 0 }; point.x = 5; and later: Mutability is a property of the binding, not of the structure itself. This seems counter-intuitive to me because point.x = 5…
Kelvin
  • 17,790
  • 2
  • 54
  • 62
12
votes
2 answers

Laravel 5 mutators only work when I create a record and not when I update a record

Hi I have created a mutator to only store digits on my phone numbers. Here is my code in my Profile Model. public function setPhoneAttribute($phone) { $this->attributes['phone'] = preg_replace("/[^0-9]/","",$phone); } This works when I create a…
OsvyG
  • 315
  • 2
  • 8
7
votes
3 answers

C++ function in parent return child

I would like to have the mutators (setters) in my class to return this to allow for jQuery-like a.name("something").address("somethingelse"); I have a parent class (Entity) and several childclasses (Client, Agent etc.). The mutators for most things…
Asmodiel
  • 836
  • 1
  • 10
  • 21
7
votes
9 answers

Does it make sense to provide non-const reference getter

Sometimes I need to expose some of the class members. For example in the following example class Mechanic may need direct access to Engine component. I have read many times that all fields should be accessed by mutator (accessor) methods because of…
doc
  • 7,500
  • 5
  • 42
  • 67
7
votes
2 answers

Java - Using Accessor and Mutator methods

I am working on a homework assignment. I am confused on how it should be done. The question is: Create a class called IDCard that contains a person's name, ID number, and the name of a file containing the person's photogrpah. Write accessor…
Pichu
  • 265
  • 2
  • 4
  • 9
6
votes
1 answer

Mutator in tabulator not working on same edited functions

I have a scenario where I am defining the mutators below and when I edit the cell it does not work where common mutators are used? I guess this is a bug or is there any other way to do it? var diffMutator_FEcontacted = function (value, data, type,…
John
  • 117
  • 9
6
votes
2 answers

How to fix it Call to a member function diffForHumans() on string in laravel 5.3

Why when I use query builder instead there is an error on the function diffForHumans (), but if I use ELoquent ROM but no error there is a way to overcome it? (How can i fix it) thank you diffForHumans() this is ArticlesController.php namespace…
Raka Hikmah
  • 65
  • 1
  • 1
  • 6
6
votes
1 answer

Creating dynamically named mutators in Laravel Eloquent models

I have a list of date fields, and all of them have the same logic in their mutators. I would like to extract this functionality to a trait so that in the future all I would need is to create an array of date fields in the model and use the…
shaswa
  • 215
  • 4
  • 14
6
votes
2 answers

Self-mutate Swift struct in background thread

Assume we have a struct capable of self-mutation that has to happen as part of a background operation: struct Thing { var something = 0 mutating func operation(block: () -> Void) { // Start some background operation …
Arnold
  • 2,278
  • 1
  • 26
  • 43
6
votes
2 answers

Python dictionary "plus-equal" behavior

I'm trying to understand the exact mechanism behind updating a python dictionary using d[key] += diff. I have some helper classes to trace magic method invocations: class sdict(dict): def __setitem__(self, *args, **kargs): print…
moatra
  • 513
  • 4
  • 9
5
votes
2 answers

What are Mutators and Accessors in Laravel

I am trying to understand accessors & mutators and why I need them. And my another ask is the middle part of an attribute's method for an example: Accessor: public function getFirstNameAttribute($value) { return…
user9551950
5
votes
6 answers

scheme functions that "remember" values with let/set

I'm new to Scheme and trying to understand how certain values that appear within a function can persist across multiple uses. Take the following counter: (define count (let ((next 0)) (lambda () (let ((v next)) (set! next (+…
planarian
  • 1,877
  • 16
  • 17
4
votes
2 answers

How does one know if a certain method will change the state of a java object?

Some methods are mutator methods, usually they return nothing, the so-called setters. Others, like the .plusDays() method of the LocalDate class, return a full, instantiated object of type Localdate, so if you want to change the object, you need to…
1
2 3
14 15