On Wednesday, 26 February 2020 at 09:45:55 UTC, Walter Bright wrote:
On 2/25/2020 1:36 AM, aliak wrote:
This may have already been answered in the other threads, but I was just wondering if anyone managed to propose a way to avoid this scenario with DIP1027?

void f(string s, int i = 0);
f(i"hello $a"); // silent unwanted bahviour.

?

It is lowered to:

  f("hello %s", a);

as designed. I don't know what's unwanted about it.

In all other languages with string interpolation that I'm familiar with, `a` is not passed to the `i` parameter.

---
C#

Code:

    public class Program
    {
        public static void f(string s, int i = 21)
        {
            System.Console.WriteLine($"s='{s}' | i='{i}'");
        }

        public static void Main()
        {
            int a = 42;
            f($"hello {a}");
        }
    }

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/ZigzagStickyHardware
---

JavaScript

Code:
    function f(s, i = 21) {
      console.log(`s='${s}' | i='${i}'`);
    }

    const a = 42;
    f(`hello ${a}`);

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/TechnologicalJointDisassembler

---

Python

Code:
    def f(s, i = 21):
        print(f"s='{s}' | i='{i}'")

    a = 42;
    f(f"hello {a}");

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/CrookedOutlandishInstructions

---

Ruby:

Code:
    def f(s, i = 21)
      puts "s='#{s}' | i='#{i}'"
    end

    a = 42;
    f("hello #{a}")

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/MidnightblueProudAgent

---

Kotlin

Code:
    fun f(s: String, i: Int = 21) {
        println("s='$s' | i='$i'");
    }

    val a = 42;
    f("hello $a");

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/ImpartialPepperyProducts

---

Dart

Code:
    void f(String s, [int i = 21]) {
      print("s='${s}' | i='${i}'");
    }

    void main() {
      const a = 42;
      f("hello ${a}");
    }

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/AwareSqueakyProlog

---

Swift

Code:
    func f(_ s: String, _ i: Int = 21) {
      print("s='\(s)' | i='\(i)'");
    }


    let a = 42
    f("hello \(a)")

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/MulticoloredCulturedRule

---

Julia:

Code:
    function f(s, i = 21)
      print("s='$s' | i='$i'")
    end

    a = 42
    f("hello $a")

Output:
    s='hello 42' | i='21'

Try it online: https://repl.it/repls/StupidAcidicDatabases

---

And so on...

Reply via email to