Function call with quotes instead of parenthesis

Hello,

I was looking at the LocalRegistry package when I noticed the following (unrelated I think to the actual LocalRegistry package).

using Pkg
pkg"registry add <repository url>"

It looks like the quotes are used instead of parenthesis? I guess I was expecting

pkg("registry add <repository url>")

But if I do ? pkg it tells me that pkg is not a function.

What is this syntax? I googled around a bit but might’ve missed it in the manual.

These are called “non-standard string literals”.

What you are seeing is a string ("registry add <repository url>") that is being passed to a macro (@pkg_str). This is a … slightly strange usage of this syntax sugar which is typically used for processing a string into something string-like (ie a regex, byte array, etc).

The main difference between a macro like this and a function is that the macro processing occurs once - during compilation, the function processing occurs every time it’s called. So using r"regex" allows it to be precompiled.

4 Likes

I assume it is supposed to mirror the syntax from the package REPL mode so that typing pkg"add SomePackage" or whatever other operations, is equivalent to activating the Pkg mode (by typing ] and getting the pkg> prompt) and just writing

pkg> add SomePackage
1 Like

That’s exactly what it does.

For the record, to add a registry with an API call requires

Registry.add(RegistrySpec(url = repository_url))

which I find too annoying even for something that can just be copy-and-pasted.

This will improve with Simplify Pkg.Registry APIs. by GunnarFarneback · Pull Request #3785 · JuliaLang/Pkg.jl · GitHub so it can be written

Registry.add(url = repository_url)

which I will change to in the LocalRegistry documentation once that feature is generally available.

3 Likes