Path and URI literals - Would they make sense?

Hi people,

I had the idea that literals for paths and URIs could be added to Scala. I don’t expect that that will ever happen. Well, who knows?

So what I imaging is that a path literal e.g. “/home/myUser/” would become an instance of java.nio.file.Path and a URI like “https://users.scala-lang.org/” would become an instance of a proper URI type (not sure about java.net.URI).

The target is to ease and short IO code, while still staying type safe. Personally I would not hard code many paths and URIs in professional software but, outsource them to a config file or something else. Still I think those literals could be useful for scripting and rapid prototyping.

I never wrote a compiler so I can’t judge if the parsing of a URI inside Scala code would be problematic. What I see is that both contain dots and colons which could possibly make thing difficult.

Maybe it’s easier with the paths where I can think of that paths must start with / for absolute paths and with ./ for relative paths. Unfortunately paths on windows look different as they use backslash for separation and IIRC can start with “c:/” and similar things.

What is your opinion? Would those literals be useful? Would the implementation be problematic?

P.S.: I never used macros. Can macros only use known scala symbols / constructs or could they rewrite a literal to code that instantiates e.g. java.nio.Path?

http4s has a macro to validate Uri a similar one for Path may already exist in another library.

By all means, I personally don’t think the compiler is a good place for this kind of addition.

1 Like

Note the literally library simplifies defining your own interpolator based literals for both Scala 2 and Scala 3 – this powers the literals in ip4s, http4s, and other libraries.

5 Likes

+1 for literally. I have used it in a few projects and it works great.