Cannot figure out NimbleCSV parser

In the first code snippet in the NimbleCSV Docs

NimbleCSV.define(MyParser, separator: “\t”, escape: “"”)

I don’t know where the MyParser variable comes form, kindly guide me

FYI, I’m new to elixir and still figuring out its ways

Hello and welcome to the forums!

NimbleCSV uses a pattern that isn’t too common amongst libraries so your confusion is certainly understandable. MyParser is not a variable but an atom. What that line is doing is actually defining a module called MyParser which you can then use like so: MyParser.parse_string("foo,bar"). You can call MyParser whatever you want.

7 Likes

MyParser isn’t a variable. It’s a module name (module names are also atoms).

The docs for nimble_csv show that NimbleCsv.define/2 actually defines a new module, using the name you provide (in the example, its MyParser) that implements a parser using the options you pass.

Thats why you call MyParser.parse_string/1 to do the actual parsing of some CSV data. MyParser is just a regular Elixir module that just happened to be generated for you.

7 Likes