CorrespondenceMutator + assignment to item operator
See original GitHub issue[@lucaswerkmeister] Summary: Polymorphic operator support for array[index] = val; map[key] = item;
.
I suggest that we add the following interfaces
shared interface CorrespondenceMutator<in Key,in Item>
given Key satisfies Object {
shared formal void set(Key key, Item item);
}
shared interface MutableCorrespondence<in Key,Item>
satisfies Correspondence<Key,Item> & CorrespondenceMutator<Key,Item>
given Key satisfies Object {}
and define that for
- any expression
cor
of typeCorrespondenceMutator<Key, Item>
,- (given that
cor
is on the operator precedence level of “member invocation and selection, index, subrange, postfix increment, postfix decrement” or a Primary, i. e., that it’s syntactically sound to writecor[key]
)
- (given that
- any expression
key
of typeKey
, and - any expression
item
of typeItem
,
an assignment operator expression with the left-hand side cor[key]
and the right-hand side item
is syntactical sugar for cor.set(key, newValue)
, where newValue
is
item
for an “assign” expression (=
)oldValue OP item
for any of the other assignment expressions (+=
,*=
,&=
, etc.), whereoldValue
iscor[key]
,cor
must therefore also be of typeCorrespondence<Key, Item>
(typicallyMutableCorrespondence<Key, Item>
),OP
is the operator of the assignment (+
,*
,&
, etc.), and- that operator therefore places additional restrictions on the type of
Item
(Summable
,Numeric
,Set
, etc.),
and that this also “carries over” to increment or decrement operators (which are already defined in terms of an “assign” expression).
This would, for example, allow code that operates on arrays to look very similar to its Java counterpart:
array[index] = val;
array[index += step] += increment;
but it would also be useful for other kinds of correspondences:
list[index] = val;
map[key] = item;
References:
- C♯ supports this via “Indexers”: http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx
- The
-Mutator
pattern is taken fromceylon.collection
, which hasListMutator
,SetMutator
,MapMutator
– a neat trick to allow better type parameter variance (MutableList.Element
is invariant, but aMutableList<Element>
is aList<Element>&ListMutator<Element>
, where the former offers a covariant (out
)Element
type parameter and the latter offers a contravariant (in
)Element
type parameter). https://github.com/ceylon/ceylon-sdk/blob/a827b28ad529f96e2e9ad894e872c249f6f27615/source/ceylon/collection/MutableList.ceylon#L31
[Migrated from ceylon/ceylon-spec#1042]
Issue Analytics
- State:
- Created 9 years ago
- Comments:101 (92 by maintainers)
Top Results From Across the Web
CorrespondenceMutator + assignment to item operator · Issue ...
How about CorrespondenceMutator.set() returns the old item associated with the given key (like MapMutator.put() ), where null indicates no item was present.
Read more >Horstmann Chapter 2
Assignment operator : = Not used as a statement about equality; Used to change the value of a variable int luckyNumber = 13;...
Read more >Mutation operators - PIT Mutation Testing
The increments mutator will mutate increments, decrements and assignment increments and decrements of local variables (stack variables). It will replace ...
Read more >The Walrus Operator: Python 3.8 Assignment Expressions
It only checks items until it finds one that satisfies the condition. Combining the := operator and any() works by iteratively assigning each...
Read more >3.1. Variables, Fields, and Parameters - OpenDSA
An assignment statement changes the value stored in the variable, ... Usually, accessor and mutator methods work directly with the object's fields, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I mean, there’s no point having this feature if
ListMutator
isn’t aCorrespondenceMutator
.Too late @Zambonifofex #4517 😄