clojureedn

What are the uses of symbols like '$ % &' in EDN?


I am new to EDN and going through EDN spec - https://github.com/edn-format/edn

What is the use of EDN symbols like '$ % &' and how can I make use of them while reading EDN in Clojure?


Solution

  • The spec mentions Symbols, but they don't mean it as what say on a keyboard is referred to as symbols.

    Its a bit confusing, so let me re-frame it. On a keyboard for example, someone might say that there are symbols, and those would be @, #, $, %, ^, &, among others. Lets call these character symbols.

    Now in EDN, you have Symbols, but it's not the referring to a character symbol. It refers to a data-type. What's even more confusing, is that it mentions that an EDN Symbol can contain a certain set of character symbol, but it is not a character symbol.

    So what are EDN symbols? Here's some:

    hello
    abc
    +
    person/name
    this$is#insane
    

    Each of these is a valid EDN Symbol. It helps to contrast them to understand them. so here are a bunch of EDN Strings:

    "hello"
    "abc"
    "+"
    "person/name"
    "this$is#insane"
    

    And here's a bunch of EDN keywords:

    :hello
    :abc
    :+
    :person/name
    :this$is#insane
    

    So what distinguishes these? Well you see, EDN Symbols, Strings and Keywords are all just a set of characters, depending if it is a Symbol, String or Keyword, the allowed characters differ, and for example, that's why the EDN spec says that a Symbol can contain certain characters like $ and ?. But it does not mention all characters, for example: ^ is not allowed in a Symbol, but it is in a String:

    hello^john ; Not a valid EDN symbol
    "hello^john" ; A valid EDN string
    

    What else, you can see that an EDN string must have the set of characters enclosed between open and closing double quotes "". On the other hand, a keyword must have the set of characters starting with a colon :. And a symbol doesn't need any marker, any continuous set of valid characters are a symbol, as long as they don't begin with : or are enclosed in double quotes.

    Now the second thing to understand is... what are they for? This is more nebulous. They are for whatever you want to use them for when you model your data as EDN. You could use EDN strings instead, or EDN keyword instead, and vice versa. Anytime you have a set of characters that only contain allowed symbol characters you could choose to use a symbol to represent them in EDN.

    In general, people use keywords for keys of maps or for tagging, such as saying that the type of animal is :monkey:

    {:animal-type :monkey}
    

    And in general, string is used to represent free-form text. Text entered by a user, or needing to be displayed to a user.

    {:animal-type :monkey
     :animal-name "Bruno the monkey"}
    

    Finally, Symbols are normally used to refer to other objects within the language itself. Such as referring to a function, another piece of data, etc.

    {:animal-type :monkey
     :animal-name "Bruno the monkey"
     :transform-fn animal/add-owner-info}