Understanding source code - new_directive vs NamedTuple in beancount.core

33 views
Skip to first unread message

denis....@gmail.com

unread,
Jun 22, 2017, 8:17:27 AM6/22/17
to Beancount
TL/DR: what's the purpose of new_directive() wrapper around NamedTuple()?

I've been following Beancount for a while, and I wanted to fork the project and make some modifications for my own use and maybe contribute back to the project at some point. But before that I want to get a good understanding of the source code. I was going through the basic data structures, and I'm trying to understand the new_directive factory. In beancount.core some classes (e.g. Posting) are created via a NamedTuple(), while others (e.g. Transaction) are created via new_directive(). new_directive() itself is a thin wrapper around NamedTuple() anyway.

So I would like to know what is the rationale for using one over the other, or for new_directive() existence in general?

Martin Blais

unread,
Jun 22, 2017, 10:12:23 PM6/22/17
to Beancount
It's just a convenience function that avoids having to repeat the common attributes "meta" and "date".
It also makes it clear all directives are guaranteed to have those attributes.

(I tend to use functional programming principles in my programming and Beancount's source code is an example of that; I don't believe that "functional" is an attribute of the language one uses - though certainly some languages offers more support than others to craft functional programs - but rather that it is an attribute of the patterns of data mutation one utilizes (or avoids).)

denis....@gmail.com

unread,
Jun 23, 2017, 5:25:38 AM6/23/17
to Beancount
Thank you Martin, that makes sense. 

What is the reason for Posting not following this? My guess is that it's because postings don't really exist as separate "objects" and are only found with Transactions, which can have dates and meta attributes. Is this the right interpretation?

Martin Blais

unread,
Jun 23, 2017, 5:34:16 AM6/23/17
to Beancount
Yes
Postings are not directives
Now they happen to also sport metadata but that was not always the case 


--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+unsubscribe@googlegroups.com.
To post to this group, send email to bean...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/f4ffac68-6424-45f0-b20e-e2ad63742c2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages