Giter Club home page Giter Club logo

Comments (5)

ksromanov avatar ksromanov commented on September 26, 2024

Thanks for the suggestion. Yes, it makes sense.This is especially useful for C++ codegeneration like

let emptyTemplateTClass name =
  [%string.padded
     {|template <class T> class $name {
           $name() {}
           ~$name() {}
       };
      |}]
      
 let emptyTemplateTClass2 name =
  [%string.padded
     {|
       template <class T> class $name {
           $name() {}
           ~$name() {}
       };
      |}]
      
 let emptyTemplateTClass3 name =
  [%string.padded
     {|
      |template <class T> class $name {
      |    $name() {}
      |    ~$name() {}
      |};
      |}]
      
 let emptyTemplateTClass4 name =
  [%string.padded
     {|
      |template <class T> class $name {
           $name() {}
           ~$name() {}
       };
      |}]

 let emptyTemplateTClass5 name =
  [%string.padded "
      |template <class T> class $name {
           $name() {}
           ~$name() {}
       };
      "]
 
 let emptyTemplateTClass6 name =
  [%string.padded
     "
       template <class T> class $name {
           $name() {}
           ~$name() {}
       };
      "]

producing

template <class T> class $name {
     $name() {}
     ~$name() {}
};

I am not sure though if .trimmed is a good suffix. May be '.indented'?

Another question is how this would interact with ecosystem. Especially formatters.

from ppx_string_interpolation.

joprice avatar joprice commented on September 26, 2024

I agree. I didn't like it when I wrote it. string.indented sounds good, also string.strip_leading, string.remove_padding, string.strip_margin. The last one is the terminology scala uses, but more specifically means that there is a character that is used to designate where the margin starts, which means the whitespace can differ per line.

Also, I haven't checked the implementation of the ppx to know if this could happen at compile time, but the scala version I mentioned is actually a runtime implementation: https://github.com/scala/scala/blob/v2.13.6/src/library/scala/collection/StringOps.scala#L765-L784. If these strings are created in a tight loop, maybe you would benefit from the compiler preprocessing them, but if they are created once at startup, then this can just be a library function instead and doesn't have to be in a ppx.

from ppx_string_interpolation.

ksromanov avatar ksromanov commented on September 26, 2024

I would prefer to use the name without verb, since it makes it lengthy and harder to type (people usually want to type less, if possible). So, string.padded or smth like that?

If it is possible, i.e. there are no blockers, which I do not see right now, it should be compile-time implementation, since it is even easier to implement than run-time.

I am mostly worried about the interaction with the ecosystem, especially formatters, which can rewrite many things and move the leading {|. I would prefer to have a clean simple semantics + minimum of visual noise. Therefore your first variant is better than Scala-like, though the latter is "formatter-proof".

I will think about this sometime next week if you do not mind - we are quite busy now.

from ppx_string_interpolation.

joprice avatar joprice commented on September 26, 2024

No urgency on my end. It would be a convenient addition but not strictly necessary.

from ppx_string_interpolation.

ksromanov avatar ksromanov commented on September 26, 2024

We considered several variants of formatting with my colleague @gxmas , so I changed my comment above and included all of them. The ideal seems to be №2 (unfortunately it does not work with ocamlformat).

I have checked the behavior of ocamlformat. It can change the formatting inside the extension point like [%string] and move the first line of the multiline string. However, it can't move the second and subsequent lines of this string, because it changes semantics.

Also, Ocaml lexer does not extract extension point as a whole - see https://github.com/ocaml/ocaml/blob/trunk/parsing/lexer.mll#L553

This means, that Ocaml parser takes the payload as a stream of tokens, so the number of spaces and newlines between these tokens can be arbitrary - https://github.com/ocaml/ocaml/blob/trunk/parsing/parser.mly#L3855

In other words, ocamlformat can (and does for some profiles) legitimately rewrite

[%string 
"   Hello
    wonderful
    world!"]

into

[%string "    Hello
    wonderful
    world!"]

and back.

This means that we are:

  1. Bound to completely ignore the first line of the string (line with "-----" is ignored):
[%string.padded {|-----
    Hello World! |}] == "    Hello World! "
  1. We can either use complete Scala solution (see №3 above), or we can use one extra pipe in the beginning (№4 and №5 above), or use №2, but take into account only the position of the closing pipe (in |}).

@joprice what do you think about these variants?

I'll try to make a proof of concept of №4 within next couple weeks.

from ppx_string_interpolation.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.