question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 type CorrespondenceMutator<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 write cor[key])
  • any expression key of type Key, and
  • any expression item of type Item,

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.), where
    • oldValue is cor[key],
    • cor must therefore also be of type Correspondence<Key, Item> (typically MutableCorrespondence<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:

[Migrated from ceylon/ceylon-spec#1042]

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:101 (92 by maintainers)

github_iconTop GitHub Comments

2reactions
gavinkingcommented, Aug 19, 2016

I mean, there’s no point having this feature if ListMutator isn’t a CorrespondenceMutator.

2reactions
jvasileffcommented, Aug 17, 2016

May I open a different issue asking for this to change?

Too late @Zambonifofex #4517 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found