Thanks, Andrew, for your feedback. I didn't even think about string 
**suffixes**, but
clearly they can be implemented together with the prefixes for additional 
flexibility.
And your idea that `<string literal> <suffix>` is conceptually no different than
`<numeric literal> <suffix>` is absolutely insightful.

Speaking of string suffixes, flags on regular expressions immediately come to 
mind.
For example `rx"(abc)"ig` could create a regular expression that performs 
global 
case-insensitive search.

> I don’t think you can fairly discuss this idea without getting at least a
> _little_ bit into the implementation details.

Right. So, the first question to answer is what the compiler should do when it 
sees
a prefixed (suffixed) string? That is, what byte-code should be emitted when the
compiler sees `lambda: a"bcd"e` ?

In one approach, we'd want this expression to be evaluated at compile time, 
similar
to how f-strings work. However, how would the compiler know what prefix "a" 
means
exactly? There has to be some kind of directive to tell the compiler that. For 
example,
imagine the compiler sees near the top of the file

    #pragma from mymodule import a

It would then import the symbol `a`, call `a("bcd", suffix="e")`. This would 
return an
AST tree that will be plugged in place of the original string.

This solution allows maximum efficiency, but seems inflexible and deeply 
invasive.

Another approach would defer the construction of objects to compile time. Though
not as efficient, it would allow loading prefixes at run-time. In this case 
`a"bcd"e` can
be interpreted by the compiler as if it was

    a("bcd", suffix="e")

where symbol `a` is to be looked up in the local/global scope.

One thing I would rather want to avoid though, is the pollution of the variable 
namespace. For example, I'd like to be able to use variable `t = ...`, without
worrying about string prefix `t"..."`. For this approach to work, we'd create a
new code op, so that `a"bcd"e` would become

    0 LOAD_CONST        1 ('a', 'bcd', 'e')
    2 STR_RESOLVE_TAG   0

where `STR_RESOLVE_TAG` would effectively call `__resolve_tag__()` special
method. The method would search for `a` in the registry of known string tags,
and then pass the tuple to the corresponding constructor. 

There will, of course, be a method to register new tags. Something like 
    
    str.___register_tag__('a', MyAObject)

As for suffix-only literals, we can treat them as if they begin with an 
underscore.
Thus, `1/3f` would be equivalent to

    1/_f(3)
_______________________________________________
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/QRVFELR44IU7H4IIGOSDBONKQJGI547P/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to