Fork me on GitHub
#clojure
<
2024-08-10
phronmophobic20:08:27

Is a keyword that starts with a number valid? (eg. :2d) > Keywords follow the rules of symbols, except they can (and must) begin with : and from symbols: > Symbols begin with a non-numeric character I can't tell if the restrictions on the first characters of symbols is overridden by the : prefix or if it instead applies to the second character of the keyword. I realize that the clojure reader and clojure.edn reader both accept :2d. However, I'm trying to figure out if using keywords like this will be forwards compatible and compatible with any edn reader.

hiredman20:08:55

There was an attempt to make those keywords unreadable a few years back, but it got reverted because it broke stuff

hiredman20:08:36

I think there may be some inconsistencies in the support of numeric keywords as well, like depending if they have a namespace component or not you get different behavior (not sure though)

phronmophobic20:08:55

The https://clojure.org/reference/reader#_literals reader docs seem similarly ambiguous on this issue.

hiredman20:08:48

I think the position is something like "would rather not allow it, but unwilling to break code that relies on it"

phronmophobic21:08:23

It's clear that patch is trying to rule out :500, but it's not clear (to me) if :5withletters is even desired to be acceptable in any of the docs.

borkdude21:08:55

maybe a good rule of thumb is, if it's an invalid symbol, it should probably be regarded as an invalid keyword?

borkdude21:08:33

I've seen numeric keywords used in deps.edn aliases etc for different clojure versions {:aliases {:1.11 ...}}

borkdude21:08:38

The reader docs say, keywords are like symbols, except ...

borkdude21:08:13

so I'd say, the same rules apply to the namespace / name

phronmophobic21:08:28

> maybe a good rule of thumb is, if it's an invalid symbol, it should probably be regarded as an invalid keyword? "it should" presumable refers to the name of the keyword?

hiredman21:08:47

Valid/invalid isn't great terminology, would be better to use readable/unreadable

seancorfield21:08:18

FWIW:

user=> :1
:1
user=> :1/a
:1/a
user=> :a/1
Syntax error reading source at (REPL:4:0).
Invalid token: :a/1
user=> :1a
:1a
None of those follow the rules but only one is unreadable.

borkdude21:08:21

well, valid/invalid according to the docs, is what I meant. readable depends on the implementation of the reader

hiredman21:08:53

No, the docs you are pointing to are intended to be reader docs

phronmophobic21:08:09

> Valid/invalid isn't great terminology Valid/invalid is great terminology. I'm looking for something that is forward compatible and expected to be readable by a wide variety of readers.

hiredman21:08:39

The keyword function will make a keyword that prints as :///500 and that keyword is a real valid keyword

hiredman21:08:51

Just not readable

phronmophobic21:08:32

I'm specifically referring to edn readers.

hiredman21:08:08

Sure, what make invalid/valid bad is because it is easy to confuse the context of that validity, in a discussion about readers valid/invalid makes sense, but it seems like already, in this context about readers, it can still be confusing what invalid/valid mean

hiredman21:08:37

Readable/unreadable carry more of their context with them

phronmophobic22:08:03

Maybe, but just because something is readable now in a particular context doesn't help with knowing whether it will be readable in many contexts now and in the future (which is what I'm actually interested in).

seancorfield22:08:39

I think there's enough prior art out there of keywords that start with a number that no usable tooling is going to appear that doesn't support them, even tho' they are "invalid", so they will always be readable at this point.

hiredman22:08:12

The distinction there is what the spec says is readable, vs what readers support reading

phronmophobic22:08:08

The quotes in the original question are from the edn spec.

hiredman22:08:09

And those are different sets, so what the spec says is unreadable as a keyword is not the same set as what a given reader may treat as unreadable

hiredman22:08:43

The smallest of those sets is what the spec defines as readable keywords, which does not include keywords that start with numbers

phronmophobic22:08:27

> Keywords follow the rules of symbols, except they can (and must) begin with : I think it's at least a little bit ambiguous as to whether :5withnumbers starts with a number since it actually starts with a :.

hiredman22:08:49

I think the language is ambiguous, but the intent is clear in the context of writing a recursive descent parser (like the reader)

phronmophobic22:08:13

Presumably, the intention is that all the restrictions that apply to the first character's of symbols applies to the second character of keywords.

hiredman22:08:52

: says that what follows is a keyword, and then you can parse what follows as a symbol, and then transform that symbol into a keyword

hiredman00:08:02

The spec for symbols themselves is written in such a way that you can read it as taking 5foo as not readable and bar/5foo as readable, because bar/5foo doesn't start with a numeric character, but I don't think any existing reader allows that

phronmophobic00:08:06

I'm never using keywords again.

😂 36
hiredman01:08:42

Well, that is also another kettle of fish again because the reader spec mentions treating symbols with . in specially, but actually that isn't something the reader does(iirc), that is part of the evaluator / compiler, so maybe doesn't belong in the reader spec at all

hiredman01:08:16

I think if I were trying to make the read spec more rigorous I would start with defining something like a "readable simple symbol" which is a symbol that is not namespace qualified, and then say a "readable symbol" is a pair of simple symbols separated by a / and a keyword is : or :: followed by a symbol. I think that would make the definitions a fair bit more rigorous while maximally preserving the intent of what is there now. The trade off is introducing the synthetic concept of a "simple readable symbol", but a similar concept has already been useful enough to introduce clojure.core/simple-symbol?

seancorfield02:08:12

And ident? / simple-ident? too. For symbols or keywords.

ghaskins22:08:34

Is there an equivalent to ‘lein install’ in Clojure CLI? Ultimately, I want to consume a jar from a deps.edn project in a lein project, so consuming it as a maven coordinate from ~/.m2 would work, but I’m sure there are plenty of ways to do this.

ghaskins22:08:12

I realized my description was a bit ambiguous. I have a library managed with deps.edn that I want to consume from a lein-managed application.

seancorfield22:08:30

If you look at the build.clj in a project generated by deps-new, you'll see an install function for that...

❤️ 18
seancorfield22:08:57

It uses deps-deploy

ghaskins22:08:13

Thanks @U04V70XH6, I’ll check that out. This is related to the cache conversation we had the other day. I see the project has a pom.xml too so maybe that’s taking me in the right direction. I’ll poke around

seancorfield22:08:10

You have an old version of deps-new if it produces a project with a pom.xml file...

seancorfield22:08:53

Yes, all Contrib projects historically used Maven

ghaskins22:08:55

I’m just trying to test out the locking hacks I added the other day in a different project

seancorfield23:08:30

The deps.edn files in Contrib projects are a convenience -- not how the projects are built.

👍 1