Most active commenters

    ←back to thread

    620 points tambourine_man | 12 comments | | HN request time: 1.076s | source | bottom
    1. runekaagaard ◴[] No.43749575[source]
    It feels a bit like "cheating" that new x-string features are built-in only. It would be cool to be able to do:

        from foo import bar
        bar"zoop"
    replies(7): >>43749594 #>>43749601 #>>43749709 #>>43750748 #>>43751130 #>>43752217 #>>43757083 #
    2. masklinn ◴[] No.43749594[source]
    A t-string is a literal for a Template object which is a data holder, it doesn't actually do anything, so you would simply call

        bar(t"zoop")
    3. Timwi ◴[] No.43749601[source]
    True. Then you could use

      sql"..."
      html"..."
    
    for each of the given examples and achieve some runtime type safety.
    replies(1): >>43749909 #
    4. thund ◴[] No.43749709[source]
    Use a function?

        bar(“zoop”)
    
    It’s just syntax, like we used to have

        print “foo” 
    
    that later became

        print(“foo”)
    5. jbverschoor ◴[] No.43749909[source]
    And you'd end up with almost no improvement.

    If you pass a "t-string" to a framework, it can force escaping.

    What you suggest is to rely on escaping by the user (dev), who, if he was aware, would already escape.

    Unless you'd suggest that it would still return a template, but tagged with a language.

    replies(2): >>43755179 #>>43810470 #
    6. cb321 ◴[] No.43750748[source]
    This is exactly how Nim is. The f-string like equivalent uses a macro called "fmt" which has a short alias "&". So you can say:

        import std/strformat
        let world = "planet"
        echo &"hello {world}"
    
    The regular expression module does a similar thing with a `re"regular expression"` syntax or std/pegs with peg"parsing expression grammar" and so on. There are probably numerous other examples.

    In general, with user-defined operators and templates and macros, Nim has all kinds of Lisp-like facilities to extend the language, but with a more static focus which helps for both correctness and performance.

    7. nhumrich ◴[] No.43751130[source]
    This was the original proposed idea in the PEP (750), but it changed overtime. There is a section in the PEP to explain why it changed to t-strings if you are interested.
    replies(1): >>43757093 #
    8. dec0dedab0de ◴[] No.43752217[source]
    meh, the difference between bar"zoop" and bar("zoop") isn't really big enough to be worth it.

    I like F strings a lot, but for the most part I think all of the various X-strings should just be classes that take a string as an argument.

    9. mcintyre1994 ◴[] No.43755179{3}[source]
    FWIW the JS equivalent is a template but tagged with a language. It has all the benefits of this template, but IDEs can easily syntax highlight the string. That seems like it would be a bit trickier to do with the Python one which is a shame.
    10. zahlman ◴[] No.43757083[source]
    In my own language design (nothing public yet - need to prioritize more practical things at the moment) this is definitely on the menu. Aside from a minimal set, keywords are implemented as compile-time macros; and it's intended that they can be extended, both "natively" and in the implementation language, by writing AST-manipulation code. But the handling of arithmetic expressions, as well as the broader line/block structure, is hard-coded. (I draw inspiration from both Python and the Lisp family.)
    11. zahlman ◴[] No.43757093[source]
    PEP 638 has always seemed to me like something of a generalization of the idea. But that really feels to me like a 4.0 feature, or rather something that needs to be designed for from the beginning. (Which is why I've done a bit of that in my own mind...)
    12. Timwi ◴[] No.43810470{3}[source]
    No, you misunderstood that completely. They would still be like t-strings, but sql-strings are now a different type from html-strings. The escaping would be done by the library that offers the sql"..." functionality.