<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged literal</title>
<link>https://ask.clojure.org/index.php/tag/literal</link>
<description>Powered by Question2Answer</description>
<item>
<title>Literals for Unicode code points (and perhaps also sequences thereof)</title>
<link>https://ask.clojure.org/index.php/14875/literals-unicode-code-points-perhaps-also-sequences-thereof</link>
<description>&lt;p&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;br&gt;
&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.oracle.com/technical-resources/articles/javase/supplementary.html&quot;&gt;For historical reasons&lt;/a&gt; the JVM type system&#039;s support for Unicode code points is poor, and while this is usually invisible to the developer it becomes a hassle when String literals containing non-Latin1 code points are used in code.  It also becomes particularly problematic when cross-platform (cljc) code is attempting to do this, since other platforms may not share this historical oddity so solutions that &quot;work&quot; in ClojureJVM may break in other dialects.&lt;/p&gt;
&lt;p&gt;For example, the &lt;a rel=&quot;nofollow&quot; href=&quot;https://emojipedia.org/transgender-flag#technical&quot;&gt;transgender flag emoji&lt;/a&gt; (a single grapheme cluster that happens to be defined by 5 Unicode code points) cannot easily be constructed at the REPL or in a source file without detailed knowledge of the JVM&#039;s history (and associated knowledge of UTF-16, an increasingly obsolete character encoding).&lt;/p&gt;
&lt;p&gt;Using the documented code points for this grapheme cluster with the JVM&#039;s Unicode escaping mechanism does &lt;em&gt;not&lt;/em&gt; give the expected outcome:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://i.ibb.co/gM2XsWBG/Screenshot-2026-01-12-at-11-13-38-AM.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;The correct, but unintuitive solution is to remember that the JVM does not directly support Unicode code points in the supplemental planes, and then to translate the supplemental code point &lt;code&gt;U+1F3F3&lt;/code&gt; into its UTF-16 code unit / surrogate pair representation:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://i.ibb.co/60b1ZLXh/Screenshot-2026-01-12-at-11-15-56-AM.png&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;Note: I had to use screenshots for this, since ask.clojure doesn&#039;t appear to support Unicode supplemental code points properly either...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Question/request/proposal&lt;/strong&gt;&lt;br&gt;
Clojure can sidestep this issue in a purely accretive manner, providing better consistency across the JVM and other runtimes, by adding direct support for Unicode literals.&lt;/p&gt;
&lt;p&gt;This would involve adding a new literal syntax that represents a single Unicode code point, and perhaps also a new literal syntax that represents a sequence of Unicode code points (perhaps supporting not only the novel Unicode code point literal, but also the existing Character and String literals).  Both of these new literals would produce a standard JVM (or JavaScript, or ...) String object, in whatever native encoding those objects employ on their respective platforms - after such literals are read, it&#039;s all just the extant String data type - there is no runtime impact.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;An &lt;em&gt;example&lt;/em&gt; literal syntax&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;While I am not proposing a specific syntax for these new literals here (though such a task is a necessary step), for illustrative purposes here is an example of what these literals &lt;em&gt;might&lt;/em&gt; approximately look like:&lt;/p&gt;
&lt;p&gt;Single Unicode code point literals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#U+0061&lt;/code&gt;: produces a String containing the Latin letter a: &lt;code&gt;&quot;a&quot;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#U+1F921&lt;/code&gt;: produces a String containing &lt;a rel=&quot;nofollow&quot; href=&quot;https://www.compart.com/en/unicode/U+1F921&quot;&gt;the clown emoji&lt;/a&gt; (which ask.clojure cannot display)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sequences of Unicode code point literals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;#U+[U+0061 U+0020 U+1F921]&lt;/code&gt;: produces the 3 grapheme cluster String: &lt;code&gt;&quot;a &amp;lt;clown emoji&amp;gt;&quot;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#U+[&quot;a &quot; U+1F921]&lt;/code&gt;: produces the same String, but demonstrates why it may be useful to support a mix of literals within the sequence (for readability)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#U+[\a \space U+1F921]&lt;/code&gt;: ditto&lt;/li&gt;
&lt;li&gt;&lt;code&gt;#U+[U+1F3F3 U+FE0F U+200D U+26A7 U+FE0F]&lt;/code&gt;: produces a String containing a single grapheme cluster (the transgender flag emoji)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This final example is an ideal test case, since the transgender flag emoji is a single Unicode grapheme cluster, defined by 5 Unicode code points, but on the JVM (for the historical reason listed originally) is made up of &lt;em&gt;6&lt;/em&gt; Characters.&lt;/p&gt;
&lt;p&gt;Note that the sequence literal may not be necessary, since &lt;code&gt;str&lt;/code&gt; could be used with the single code point literal syntax; e.g. &lt;code&gt;(str #U+1F3F3 #U+FE0F #U+200D #U+26A7 #U+FE0F)&lt;/code&gt;.  Whether shifting the cost of string concatenation from read-time to runtime matters or not is another topic worthy of deeper consideration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Other notes&lt;/strong&gt;&lt;br&gt;
Orthogonal to this proposal (at least from a Clojure core perspective; from a user perspective they&#039;re closely related), it would also be useful if Clojure core (perhaps in the &lt;code&gt;clojure.string&lt;/code&gt; namespace) had functions to turn Strings into sequences of code points (as integers) and vice versa.  Both the JVM and JavaScript provide native APIs for doing this (and presumably other platforms do too), but providing these as standard functions in Clojure core (similar to what was done with &lt;code&gt;parse-long&lt;/code&gt;, &lt;code&gt;parse-double&lt;/code&gt;, and &lt;code&gt;parse-boolean&lt;/code&gt; in Clojure v1.11) has value and is also purely accretive.&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14875/literals-unicode-code-points-perhaps-also-sequences-thereof</guid>
<pubDate>Mon, 12 Jan 2026 20:02:41 +0000</pubDate>
</item>
<item>
<title>Literal `()` erroneously produces `nil` in cljs.core.async `go` blocks</title>
<link>https://ask.clojure.org/index.php/11520/literal-erroneously-produces-nil-in-cljs-core-async-blocks</link>
<description>&lt;pre&gt;&lt;code&gt;(go (println () [] (range 0) (list) {} #{}))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This prints&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nil [] () () {} #{}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But it should print (which it properly does outside of go blocks)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;() [] () () {} #{}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This appears to only be an issue in ClojureScript.&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/11520/literal-erroneously-produces-nil-in-cljs-core-async-blocks</guid>
<pubDate>Sat, 29 Jan 2022 19:26:41 +0000</pubDate>
</item>
<item>
<title>A literal meta in cljs.core.async</title>
<link>https://ask.clojure.org/index.php/8966/a-literal-meta-in-cljs-core-async</link>
<description>&lt;p&gt;Is it a bug or a technical impossibility to implement a literal meta in cljs version of core.async?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns clojurescript-example
  (:require
   [cljs.core.async :as a]))

(let [x (a/go ^:foo [:x])]
  (a/go
    (prn (meta (a/&amp;lt;! x))))) ;; nil
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;(ns clojure-example
  (:require
   [clojure.core.async :as a]))

(let [x (a/go ^:foo [:x])]
  (a/go
    (prn (meta (a/&amp;lt;! x))))) ;; {:foo true}
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>core.async</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8966/a-literal-meta-in-cljs-core-async</guid>
<pubDate>Tue, 24 Dec 2019 20:02:54 +0000</pubDate>
</item>
<item>
<title>Add text block literal, or raw string literal, or unsescaped string literal.</title>
<link>https://ask.clojure.org/index.php/8520/text-block-literal-string-literal-unsescaped-string-literal</link>
<description>&lt;h2&gt;Problem:&lt;/h2&gt;
&lt;p&gt;Having had to include some JavaScript, XML and HTML inside of my Clojure code here and there, it can be pretty annoying and error prone to have to escape quotes. This holds true as well when scripting, and running shell command, you can get into hairy escaping scenarios.&lt;/p&gt;
&lt;h2&gt;Solution:&lt;/h2&gt;
&lt;p&gt;Add a string literal which can be adapted to contain any sort of string without the need for escaping.&lt;/p&gt;
&lt;h2&gt;Suggestions:&lt;/h2&gt;
&lt;h3&gt;Text blocks&lt;/h3&gt;
&lt;p&gt;Some other languages offer something called a text block where you can write a string using triple or more quotes, where all characters are then allowed:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;
         This &quot; is allowed,
         and no need to escape it.
         &quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Text blocks often come with additional features, such that the first and last newline isn&#039;t part of the string. And the position of the triple quote in the source code delineate the beginning of the lines in the quote. Thus the above code prints:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;This &quot; is allowed,
and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and not:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         This &quot; is allowed,
         and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While text blocks are neat visually, as they have nice alignment in the source code. They are whitespace dependent, and Clojure up to now is a whitespace independent language, meaning whitespace does not matter. I think it would be best to keep it that way. Thus the next two suggestions.&lt;/p&gt;
&lt;h3&gt;Raw strings&lt;/h3&gt;
&lt;p&gt;Sometimes the text block without the &quot;block&quot; features is known as a raw string literal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;This &quot; is allowed,
and no need to escape it.
Also support multi-line, but
not the &quot;block&quot; style of text blocks.&quot;&quot;&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thus:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println &quot;&quot;&quot;
         This &quot; is allowed,
         and no need to escape it.
         &quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prints:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;         This &quot; is allowed,
         and no need to escape it.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unlike for Text Blocks.&lt;/p&gt;
&lt;p&gt;If you need a triple quote, just make the delimiter a quadruple quote:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;&quot;This &quot;&quot;&quot; is now allowed as well.&quot;&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The issue with raw string is that, if you use say double quotes as your delimiter:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;This is a raw &quot; string!&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But want your single quote to be at the beginning or the end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;&quot;{{hello}}&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I want the string: &lt;code&gt;&quot;{{hello}}&quot;&lt;/code&gt;, not &lt;code&gt;{{hello}}&lt;/code&gt;, but the raw string can not disambiguate the two, as now it thinks this is a triple quoted delimiter.&lt;/p&gt;
&lt;p&gt;One solution is to allow an escaped quote only at the beginning or end:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;\&quot;{{hello}}\&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But not in the middle:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;&quot;\&quot;{{he\llo}}\&quot;&quot;&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is the string: &lt;code&gt;&quot;{{he\llo}}&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;So the escape character &lt;code&gt;\&lt;/code&gt; can appear anywhere except at the beginning if followed by a quote, and at the end if followed by a quote.&lt;/p&gt;
&lt;p&gt;I still don&#039;t find this ideal. There&#039;s too many rules, and there are still cases where an escape is required.&lt;/p&gt;
&lt;h3&gt;Unescaped string (my favorite)&lt;/h3&gt;
&lt;p&gt;The idea here is to allow any string to be used as the delimiter. Thus given whatever possible string we want to nest inside our Clojure code, we can always find a string which is not contained in it to use as our delimiter.&lt;/p&gt;
&lt;p&gt;Lets say the reader macro #text is added. Which expects the following form to be a regular string which tells it the delimiter for the following form to read:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println #text &quot;|&quot; |&quot;{{hello}}&quot;|)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Would print:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&quot;{{hello}}&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The first arg to #text tells it what the delimiter for the following raw string should be. That way, you absolutely never need an escape sequence inside the raw string. For any given string, you can find a delimiter string not contained in it to handle it properly.&lt;/p&gt;
&lt;p&gt;A crazy thought I had with this approach, just trowing it out there, is if you use a sufficiently random string as the delimiter, could be a weird way to protect against forms of injection:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(println #text &quot;xIBgdSl4TCCOIdqdMu9G&quot; xIBgdSl4TCCOIdqdMu9G
Can&#039;t nobody guess the delimiter to escape the string context :p
xIBgdSl4TCCOIdqdMu9G)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thank You&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8520/text-block-literal-string-literal-unsescaped-string-literal</guid>
<pubDate>Tue, 03 Sep 2019 22:46:52 +0000</pubDate>
</item>
<item>
<title>Why do BigInt literals not require N suffix, but BigDecimals do?</title>
<link>https://ask.clojure.org/index.php/8510/why-do-bigint-literals-not-require-suffix-but-bigdecimals-do</link>
<description>&lt;p&gt;I find it a bit inconsistent that I can type:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;123423534634544234534634645342363462443649886756676645466767687
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And it will auto-promote it to a BigInt to fit, without me needing to specify so with the N suffix.&lt;/p&gt;
&lt;p&gt;But if using decimal:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;12.3423534634544234534634645342363462443649886756676645466767687
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It won&#039;t auto-promote to BigDecimal without explicitly adding an M suffix. Instead it&#039;ll truncate to double precision. &lt;/p&gt;
&lt;p&gt;Any reason why that is?&lt;/p&gt;
</description>
<category>Syntax and reader</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8510/why-do-bigint-literals-not-require-suffix-but-bigdecimals-do</guid>
<pubDate>Tue, 03 Sep 2019 19:28:00 +0000</pubDate>
</item>
</channel>
</rss>