On Fri, Nov 27, 2020 at 2:46 PM MRAB <pyt...@mrabarnett.plus.com> wrote:
>
> On 2020-11-27 02:30, Ned Batchelder wrote:
> > On 11/26/20 9:02 PM, MRAB wrote:
> >> On 2020-11-27 01:12, Ned Batchelder wrote:
> >>> On 11/26/20 6:44 AM, 3mi...@gmail.com wrote:
> >>>> Add something like Move type hint to typing module. It will tell the
> >>>> analyzer that the input parameter of the function is moved and can
> >>>> not be used after. For example:
> >>>> ```
> >>>> def f(d: Move[dict]) -> dict:
> >>>>      d['a'] = 2
> >>>>      return d
> >>>>
> >>>> d = {1: 2}
> >>>> f(d)
> >>>> print(d[1])  # mistake, using of moved value
> >>>
> >>> Maybe I'm behind the times on the latest thinking in programming
> >>> languages.  I'm not familiar with "move semantics". Can someone explain
> >>> what is wrong with the code above?  What is the mistake? `print(d[1])`
> >>> will work perfectly fine.  The function changes the 'a' key, and then we
> >>> access the 1 key. Should the example have used the same key in both
> >>> places?
> >>>
> >> d is moved into function f. f does return d when it exits, moving it
> >> out back again, but as the caller doesn't bind to it, it's discarded.
> >>
> > It's not discarded, it's still referenced by d in the outer scope.
> >
> No, it's not any more, and that's the point. It was _moved_ into the
> function, and although the function returned it, it was discarded
> because the caller didn't bind it to keep hold of it.

Currently, type hints and annotations don't change actual behaviour
like this. Is this proposal to have it actually get discarded, or
merely to have a type checker flag a warning if you continue to use
it?

I strongly disagree with the former. When you call a function, it
doesn't matter what the function is, you just call it. It's the
function's responsibility to do anything special. That means there
can't be an unbind operation in the caller on the basis of a flag on
the function.

The latter is also more closely aligned with the way that Python
currently handles "oops you used this after it should have been
discarded". Consider:

>>> d = {1:2, 3:4}
>>> i = iter(d)
>>> next(i)
1
>>> del d[1]
>>> next(i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration
>>> i
<dict_keyiterator object at 0x7f28c19244f0>

The iterator still exists, but has been internally flagged as "hey,
this thing is broken now, don't use it". If that's the meaning of
"move semantics" (imagine that "del d[1]" is replaced with some
function that is listed as 'consuming' i), then that does at least
make reasonable sense.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MVFTGZ3TJXVXRBD2O6IFEDTARM5I5AT2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to