@@ -981,13 +981,13 @@ block:
981
981
end
982
982
```
983
983
984
- ## Non-Standard String Literals
984
+ ## [ Non-Standard String Literals] ( @ id meta-non-standard-string-literals)
985
985
986
986
Recall from [ Strings] (@ref non-standard-string-literals) that string literals prefixed by an identifier are called non-standard
987
987
string literals, and can have different semantics than un-prefixed string literals. For example:
988
988
989
- * ` r"^\s*(?:#|$)" ` produces a regular expression object rather than a string
990
- * ` b"DATA\xff\u2200" ` is a byte array literal for ` [68,65,84,65,255,226,136,128] ` .
989
+ * ` r"^\s*(?:#|$)" ` produces a [ regular expression object] ( @ ref man-regex-literals) rather than a string
990
+ * ` b"DATA\xff\u2200" ` is a [ byte array literal] ( @ ref man-byte-array-literals) for ` [68,65,84,65,255,226,136,128] ` .
991
991
992
992
Perhaps surprisingly, these behaviors are not hard-coded into the Julia parser or compiler. Instead,
993
993
they are custom behaviors provided by a general mechanism that anyone can use: prefixed string
@@ -1051,20 +1051,9 @@ constructed on each iteration. In the vast majority of use cases, however, regul
1051
1051
are not constructed based on run-time data. In this majority of cases, the ability to write regular
1052
1052
expressions as compile-time values is invaluable.
1053
1053
1054
- Like non-standard string literals, non-standard command literals exist using a prefixed variant
1055
- of the command literal syntax. The command literal ``` custom`literal` ``` is parsed as ` @custom_cmd "literal" ` .
1056
- Julia itself does not contain any non-standard command literals, but packages can make use of
1057
- this syntax. Aside from the different syntax and the ` _cmd ` suffix instead of the ` _str ` suffix,
1058
- non-standard command literals behave exactly like non-standard string literals.
1059
-
1060
- In the event that two modules provide non-standard string or command literals with the same name,
1061
- it is possible to qualify the string or command literal with a module name. For instance, if both
1062
- ` Foo ` and ` Bar ` provide non-standard string literal ` @x_str ` , then one can write ` Foo.x"literal" `
1063
- or ` Bar.x"literal" ` to disambiguate between the two.
1064
-
1065
1054
The mechanism for user-defined string literals is deeply, profoundly powerful. Not only are Julia's
1066
- non-standard literals implemented using it, but also the command literal syntax (``` `echo "Hello, $person"` ``` )
1067
- is implemented with the following innocuous-looking macro:
1055
+ non-standard literals implemented using it, but the command literal syntax (``` `echo "Hello, $person"` ``` )
1056
+ is also implemented using the following innocuous-looking macro:
1068
1057
1069
1058
``` julia
1070
1059
macro cmd (str)
@@ -1077,6 +1066,20 @@ but they are just functions, written entirely in Julia. You can read their sourc
1077
1066
what they do -- and all they do is construct expression objects to be inserted into your program's
1078
1067
syntax tree.
1079
1068
1069
+ Like string literals, command literals can also be prefixed by an identifier
1070
+ to form what are called non-standard command literals. These command literals are parsed
1071
+ as calls to specially-named macros. For example, the syntax ``` custom`literal` ``` is parsed
1072
+ as ` @custom_cmd "literal" ` .
1073
+ Julia itself does not contain any non-standard command literals, but packages can make use of
1074
+ this syntax. Aside from the different syntax and the ` _cmd ` suffix instead of the ` _str ` suffix,
1075
+ non-standard command literals behave exactly like non-standard string literals.
1076
+
1077
+ In the event that two modules provide non-standard string or command literals with the same name,
1078
+ it is possible to qualify the string or command literal with a module name. For instance, if both
1079
+ ` Foo ` and ` Bar ` provide non-standard string literal ` @x_str ` , then one can write ` Foo.x"literal" `
1080
+ or ` Bar.x"literal" ` to disambiguate between the two.
1081
+
1082
+
1080
1083
Another way to define a macro would be like this:
1081
1084
1082
1085
``` julia
0 commit comments