-
Notifications
You must be signed in to change notification settings - Fork 62
CorrespondenceMutator + assignment to item operator #4148
Comments
[@lucaswerkmeister] Come to think about it, this is rubbish for assignment expressions other than “assign” expressions, because |
[@gdejohn] How about
|
[@lucaswerkmeister] I don't follow. We already need to know the value when calling put, how will its return value help us? -----Original Message----- How about CorrespondenceMutator.set() returns the old item associated with the given key (like MapMutator.put()), where null indicates no item was present. Then you can do all of the compound assignments, and if foo[bar] doesn't exist, nothing happens (and of course can you check for that). |
[@gdejohn] I'm saying allow |
[@luolong] I am not sure The thing is that since type of |
[@gdejohn]
I understand that, but this still wouldn't be regular even if the type of |
[@gavinking] Honestly I don't like the sound of it. Seems to be hiding just too much implicitness inside an innocuous-looking |
[@gdejohn] Yeah, that's a reasonable complaint. |
[@Zambonifofex] |
I created an implementation for this in the typechecker and the JVM backend. @gavinking could you take a look at the language/typechecker part to see if things seemokay? @chochos would you be willing to create the JS backend side for this? |
Well, that's not exactly the same, they don't test boxing or the various types of LHS. |
Btw, after I closed this @jvasileff opened #6435 which made me realize the current implementation is crap anyway. Honestly I'm getting lost in the complexity of our expression builders and have no idea what the proper way to implement this would be. That's the cause of #6435, because I somehow circumvent the correct way of generating all the proper accesses of local/class/toplevel variables. |
OK, fixed for the JVM. |
I added the interfaces as agreed, but they need documentation, can you help @gavinking ? Then @chochos needs to do the JS magic. I also fixed the jvm backend so that the code works wrt any expression and boxing/primitive types, and added backend tests also for interop. I'll open an SDK issue to add this to the SDK. |
Done in SDK: eclipse-archived/ceylon-sdk#607 |
So have we tested this for all of the following:
|
Yes. |
Thanks @chochos ! |
Actually this is not BC: we can't run |
Fixed with a terrible hack to generate a |
👍 |
[@lucaswerkmeister] Summary: Polymorphic operator support for
array[index] = val; map[key] = item;
.I suggest that we add the following interfaces
and define that for
cor
of typeCorrespondenceMutator<Key, Item>
,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]
)key
of typeKey
, anditem
of typeItem
,an assignment operator expression with the left-hand side
cor[key]
and the right-hand sideitem
is syntactical sugar forcor.set(key, newValue)
, wherenewValue
isitem
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.), andItem
(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:
but it would also be useful for other kinds of correspondences:
References:
-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]
The text was updated successfully, but these errors were encountered: