Skip to main content

Allow auto-escaped strings

6 replies [Last post]
pdoubleya
Offline
Joined: 2004-02-09

I read this idea on another forum awhile ago (don't have the link). The idea is that you add an indicator before string assignments that causes any \ to be treated as \\.

Now we do (on Windows)
File f = new File("c:\\temp\\logs\\applog.txt");

Using the "escape" marker
File f = new File(\e "c:\temp\logs\applog.txt");

I don't care what the syntax of this is, as long as it is clearly visible on scanning/reading the code.

1) Improves readability--what you see is what you get
2) Can be implemented in compiler
3) Avoids simple typing mistakes in replacing \ with \\

Patrick

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tackline
Offline
Joined: 2003-06-19

In my opinion the most significant problem with literal strings across most languages is the commonly accepted method of escaping. If you escape what is already escaped, you get something unreadable.

Regexs literals would be easier in Java if unrecognised escaped characters went straight through and \\ was interpreted as \\ instead of \. \; is a reasonable choice to represent a single backslash. So, for instance "\\\;\(\t" would be six characters: four backslashes, an open parenthesis and a tab. As it is I prefer a small method to switch forward and back slashes immediately before compiling the regex.

Similarly in XML wouldn't it be great for scriptlets if "&&" and "& " represented themselves.

The other thing is multiline string literals, which is straight forward enough. C# use @" to introduce its alternate string literal format, which is arbitrary but reasonable.

Finally, it does seem strange that with the new code point (21ish-bit Unicode) support, that there is no easy way to represent one in a string literal. Sure you can use a surrogate pair \uD###\uD###, but that's not entirely clear. \U######, perhaps.

monika_krug
Offline
Joined: 2004-10-14

Very much for it! For filenames on Windows and for regex it would be very useful.

But it is kind of hard to come up with a sensible notation for it.

Monika.

patrikbeno
Offline
Joined: 2004-10-11

-1
I don't see real reason for this.

gipak
Offline
Joined: 2004-10-11

-1

Bad idea, what about "\t", "\n" and most important "\"" etc... only place where auto-escaping "\" could be useful would be in regular expressions i guess.

monika_krug
Offline
Joined: 2004-10-14

> Bad idea, what about "\t", "\n"

Well duh, obviously one would only use auto-escaped strings if one has strings (like file names) which do not contain \t, \n etc.

Monika.

patrikbeno
Offline
Joined: 2004-10-11

just hint: you can safely use "c:/temp/logs/applog.txt" in Java.