1

Why can't we simply convert Clojure code to string and send it over TCP and evaluate on the other side(nrepl)?

For example : This is a hashmap {"foo" "bar", 1 "spam"} whose BENCODE encoding is d3:foo3:bari1e4:spame.

If we convert it to string -> {\"foo\" \"bar\", 1 \"spam\"}

and evaluate on the other side instead of using BENCODE as shown below.

(eval (read-string "{\"foo\" \"bar\", 1 \"spam\"}"))
; ⇒ {"foo" "bar", 1 "spam"}

I am new to the Clojure world. This might be a stupid question but anyway.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Our scope is explicitly limited to _practical_ questions -- ones that change how you go about the practice of programming. "Why was thing X designed in way Y?" doesn't change how you use X, and doesn't let you fix or avoid any bug -- it's merely a matter of historical note or curiosity, and thus often considered off-topic. – Charles Duffy Jan 04 '20 at 20:20
  • See [What is the rationale for closing "why" questions on language design?](https://meta.stackexchange.com/questions/170394/what-is-the-rationale-for-closing-why-questions-on-a-language-design) on [meta]. (Granted, this is wire protocol design rather than language design, but the rationale holds). – Charles Duffy Jan 04 '20 at 20:23
  • That said, insofar as bencode was designed as an extension of DJB's netstrings, you may find it useful to read the rationale for the latter, at http://cr.yp.to/proto/netstrings.txt – Charles Duffy Jan 04 '20 at 20:27
  • 2
    Your question may be considered more appropriate on sites like https://www.reddit.com/r/Clojure or https://clojureverse.org or https://ask.clojure.org – andy_fingerhut Jan 04 '20 at 20:34
  • @CharlesDuffy thanks for the answer. Also, I will take care of using StackOverflow for designated purposes only. – npcoder2k14 Jan 06 '20 at 13:38

2 Answers2

1

nREPL's maintainer here. There are several reasons why nREPL uses bencode by default:

  • We needed a data format that supports data streaming easily (you won't find many streaming JSON parsers)
  • We needed a data format that could easily be supported by many clients (support for formats like JSON and EDN is tricky in editors like Emacs and vim). I can tell you CIDER would not have existed if 8 years ago we had to deal with JSON there. :-)
  • Bencode is so simple that usually you don't even need to rely on a third-party library for it (many clients/servers have their own implementation of the encoding/decoding in less than 100 lines of code) - this means means that clients/servers have one less 3rd party library. Tools like nREPL can't really have runtime deps, as they conflict with the user application deps.
  • EDN didn't exist back when nREPL was created

Btw, these days nREPL supports EDN and JSON (via the fastlane library) as well, but I think that bencode is still the best transport in most cases.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
0

For people looking for the answer, read the # Motivation section in https://github.com/clojure/tools.nrepl/blob/master/src/main/clojure/clojure/tools/nrepl/bencode.clj

This is very well written.