Multi-dollar String Interpolation in Kotlin | $$

Поділитися
Вставка
  • Опубліковано 16 гру 2024

КОМЕНТАРІ • 22

  • @Talaria.School
    @Talaria.School 18 годин тому +2

    Thanks for the content. Nice acting ... 2:08 we defenetly found a même. My TDD red step alert.

  • @azamovdev
    @azamovdev 17 годин тому +1

    Kotlin Forever

  • @vijaymistry2181
    @vijaymistry2181 19 годин тому

    QuizyShow is made by Kotlin 🎉

  • @skarloti
    @skarloti 18 годин тому +4

    val car = "..."
    println($$"How much $$$ does this $$car cost?")
    ....
    In this case, what will happen? Many other examples can be thought of.

    • @AlanDarkworld
      @AlanDarkworld 18 годин тому

      Not quite. In your example, if the output string should be "how much $ does this...", then you would only use one $ in the string, since you've specified that $$ is your escape sequence. so it would be: println($$"How much $ does this $$car cost?"). In this case, a single '$' in the string is treated like a regular character. I'm not saying that it's ideal (I would have preferred "\$" to get a literal '$' character) but I guess this is the backwards-compatible solution.

    • @skarloti
      @skarloti 18 годин тому +1

      @@AlanDarkworld I want the literal dollar sign to be counted triple or double in the output string. I.e. if we take the double dollar as a prefix because that's how we declared it at the beginning of the string, the load may have quite a few unresolved problems.
      Moreover, this is, to put it mildly, not syntactically correct in the Kotlin language.I do not support this decision at all!!! Something more appropriate and properly supported needs to be invented.

    • @AlanDarkworld
      @AlanDarkworld 18 годин тому

      @skarloti I get where you're coming from but I'm afraid that ship has sailed. This is the solution we'll have to deal with.

    • @skarloti
      @skarloti 17 годин тому

      @@AlanDarkworldI would make a function that would do this much more successfully and would be convenient to use.

    • @AntonArhipov
      @AntonArhipov 16 годин тому

      In this example, three dollars will be printed and the 'car' variable value will be interpolated. Same if it is a double-dollar ($$) with no text after the sequence - nothing gets interpolated and double-dollar ($$) will be printed

  • @skarloti
    @skarloti 16 годин тому +2

    val city = "Madrid"
    How to println($$"$$Hello $$city")
    And the result will be!
    $$Hello Madrid

    • @AntonArhipov
      @AntonArhipov 15 годин тому

      This won't compile if there's no "Hello" variable to interpolate.

    • @skarloti
      @skarloti 15 годин тому

      @@AntonArhipov How do I print the expression verbatim?
      $$Hello Madrid
      What if we use a city variable?

    • @AntonArhipov
      @AntonArhipov 15 годин тому

      ​@@skarloti Think of it this way - the dollars (two or it can be more, even "$$$$$$$$$") in front of the string template is just a control sequence that redefines the interpolation symbol. So the example above would be implemented as follows:
      val city = "Madrid"
      println($$$"$$Hello $$$city") // prints "$$Hello Madrid"
      The double-dollar is then ignored as it's not an interpolation symbol, and 'city' variable is interpolated.

    • @skarloti
      @skarloti 15 годин тому

      @@AntonArhipov I completely understand you, but imagine that I'm not sure whether the text will contain this template or not.For example, I get a result json from ktor.
      Imagine you have to solve the problem of parsing CSV and you have the comma separators.This should not limit you to any column/row field not being present.Placing a template on your agi should have a way of how it can be used without being replaced.I'm not saying that the task is easy to solve, because you may encounter recursive and slow solutions.So my advice is not to use this template. I would make an extension function that has configurators and lambdas etc.Template creation in your case should be optimized and cached as is parsing of json, so that it can work quickly. Otherwise it will be clumsy and slow.
      Please think about this.Happy holidays

    • @AntonArhipov
      @AntonArhipov 14 годин тому

      @@skarloti I'm not sure it is even possible to interpolate strings/templates that come from the outside 🤔 In this case we really talk about something else - a fully-fledged templating engine like Velocity/Freemarker/StringTemplate would be applicable here, with caching and optimization as you said.