Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefining a record's fields in Clojure

Tags:

clojure

In The Joy of Clojure it states that:

you can even redefine a record if desired to have different fields giving you the compiled performance of Java dynamically

Can someone clue me in to how this is done?

Thanks

like image 899
pondermatic Avatar asked Jul 13 '12 03:07

pondermatic


1 Answers

In chapter 9 we walk through the definition of the FIXO protocol and extensions to both a TreeNode record and TreeNode type. We show that building up the implementation of FIXO can be done dynamically. In fact, we start with a TreeNode record, extend the FIXO protocol and then define a TreeNode type and extend FIXO to it instead. While we do not change the number of fields, we do change the type itself and once extended, all calls through the FIXO protocol resolve to the new type (we also had to redefine the FIXO extension to nil as it was returning the old record-based NodeType.

You'd likely not do this in code that you would deploy, but it's a useful technique when experimenting. Try to walk through the FIXO section and check the types as you go.

like image 53
fogus Avatar answered Oct 14 '22 01:10

fogus