Arc Forumnew | comments | leaders | submitlogin
How do you write a string literal with an at sign?
3 points by zck 3843 days ago | 2 comments
As part of my ongoing effort to make someone else use my unit test framework (https://bitbucket.org/zck/unit-test.arc/), I've been converting the Anarki unit tests to use unit-test.arc (at akkartik's suggestion).

I ran into an issue with lib/tests/core-typing-test.arc. The existing tests pass, but when converting them into my test framework, I ran into an issue:

    arc> "abc@example.com"
    _example: undefined;
     cannot reference undefined identifier
      context...:
       /home/zck/anarki/ac.scm:1227:4
It turns out the at sign starts string interpolation:

    arc> (let variable 3 "The value is @variable")
    "The value is 3"
I'm not exactly sure why the existing unit tests don't break -- there's a string literal "abc@example.com" in them -- and why my tests do. Maybe the existing test framework interprets the string literal in a lower-level context? Who knows.

Either way, I think the existence of the feature is a point worth discussing. I think it's a very confusing feature, especially since there seems to be no way to have a string literal with an ampersand:

    arc> "abc\@example.com"
    UNKNOWN::16183: read: unknown escape sequence \@ in string
      context...:
       /home/zck/anarki/ac.scm:1227:4

    arc> _com: undefined;
     cannot reference undefined identifier
      context...:
       /home/zck/anarki/ac.scm:1227:4

    arc> "abc@@example.com"
    "\n"
    arc> _abc@@example: undefined;
     cannot reference undefined identifier
      context...:
       /home/zck/anarki/ac.scm:1227:4
It also leads to output that can't be pasted back into the repl:

    arc> (string "abc" #\@ "example.com")
    "abc@example.com"
Except by cheating:

    arc> '"abc@example.com"
    "abc@example.com"
I'd prefer some way to have to opt into string interpolation, like this:

    arc> (let variable 3 @"The value is @variable") ;;this is just an example; it doesn't work anywhere
    "The value is 3"
And then a way to escape the at sign, even when it is the special string:

    arc> (let name "steve" @"My email is @name\@example.com")
    "steve@example.com"


4 points by rocketnia 3843 days ago | link

Yeah, atstrings is a pretty awkward feature. It is possible to opt in and out of it, but only at the top level:

  (declare 'atstrings t)
  (declare 'atstrings nil)
This affects how strings are processed by 'ac. That is, it only matters for compilation/macroexpansion phases.

I see some of Anarki's tests set 'atstrings to a particular value as they go along, but then they don't reset it. This might be why you see some tests working when yours don't.

(In Anarki, the declarations* global table holds these values, so it's technically possible to save and restore the old value by reading that table.)

---

The REPL behavior you observed is confusing in a different way than you thought. It treated your input as a malformed command, three well-formed commands you didn't know you were entering, and finally an unfinished string literal:

  "abc\@
  
  example.com
  
  "
  "
  
  abc@@example.com
  
  "
This is where the "\n" response came from, and it's why you received multiple responses per input line.

-----

2 points by akkartik 3842 days ago | link

I'm ambivalent about the feature as well, but you should be able to double @'s to insert a literal @. In anarki:

  arc> "abc@@example.com"
  "abc@example.com"

-----