31
3
I find it deeply weird that this is possible in Ruby (I won't immediately say how):
obj = #code redacted
print obj.state # Some value.
LValue = obj
print obj.state # Different value!
Your challenge is to create code roughly of this form. Create an object and assign it to a variable. It should have some defined attribute (or deterministic, idempotent method) like state above, that changes after the object is assigned to a new identifier (LValue above), even if you still use the old identifier (obj above) to refer to it.
Edit for emphasis: state or the equivalent must be idempotent, so creating an accessor that modifies the value, or for any other reason returns different results when called several times in a row, isn't a valid solution. Or, more simply, it has to be the assignment that changes the state.
Any language with assignment is eligible, although there are probably some where there's no fully legitimate solution. I'll post my Ruby answer if nobody else gets it after a few days, and accept highest-voted answers on a rolling basis.
Would destructive alterations to the object itself? EmacsLisp:
(setq a (list "val")) (setq b (nconc a "val2"))for example.aends up evaluating as("val" . "val2")at that point. – Jonathan Leech-Pepin – 2015-11-04T22:38:18.983It's a popularity contest, but to me returning the refcount is in the spirit of the challenge, but putting a destructive operation on the right hand side is not -- in your example, it's the nconc that's performing the mutation, not the setq (if I understand correctly). – histocrat – 2015-11-04T23:18:51.537
Must the
LValue = objline be required forstateto actually change? (I could just make a property in C# that increments every time you get it) – Tim S. – 2014-06-04T20:27:01.4232Yes, that's what I intended by saying the method needed to be idempotent. I'll edit to make that clearer. – histocrat – 2014-06-04T20:33:56.987
Ok, thanks. I must've glossed over that part. – Tim S. – 2014-06-04T20:36:20.483
4Would simply returning the refcount of the object work? – Nick T – 2014-06-05T22:51:49.133