The confusion over type and instance was a sloppy example.   Sorry.

But in your solution I'm confused by one thing.   You create and
instance of Foo in the let and then assoc the new value of List1 to
it.
This has two problems.   One is that the loaded data is not
permanent.   The other is that there is no way to add List2 to the
MyFoo record.

(I've decided to not use MyFoo at all.   Instead I have a (def List1
(ref nil)) defined and then do a ref-set to it when the data is
loaded.    This seems
much simpler and works.   I then can do a def for List2, List3, etc.)

It seem to me there should be some way to load a record at run time
without breaking the immutability laws.    Once the dynamic data is
loaded, the record becomes immutable and will never be changed
again.    It's unrealistic to imagine that we always know at compile
time what the values of a record will be.
I'm not a compiler person so I have no idea how to do such a thing and
no idea if it is possible.

On Jul 27, 5:23 pm, Stuart Halloway <stuart.hallo...@gmail.com> wrote:
> Hi Bill,
>
> There are several issues here:
>
> (1) A confusion of record and instance. You are asking for the :list1 field 
> from foo, the record type, not from an instance of the type.
>
>         (:list1 foo)   s/b  (:list1 MyFoo)
>
> (2) You are ignoring the return value of assoc. Remember, Clojure data 
> structures are immutable. To actually assign something you need to hold on to 
> the return value of the expression (or use a reference type if you really 
> want an identity).
>
> (3) The capitalization choices facilitate confusion. You are using "foo" for 
> a record/class name, where both Java and Clojure style would dictate "Foo". 
> Then you use "MyFoo" for a top-level def, where Clojure style would encourage 
> "my-foo".
>
> The following code demonstrates these ideas.
>
> ;; stubbed so example can be run
> (defn load-data-from-file
>   [x] :stub)
>
> (defrecord Foo [list1 list2])
>
> (defn get-data [path]
>   (let [list1Data (load-data-from-file path)
>         f (Foo. nil nil)
>         f (assoc f :list1 list1Data)]
>     (:list1 f)))
>
> (get-data "fakepath")
>
> Regards,
> Stu
>
> Stuart Halloway
> Clojure/corehttp://clojure.com> All the examples of defrecord I see seem 
> simple enough and when I
> > experiment in the REPL I get things to work as they should.  However,
> > when I move to 'real' code I can't get it to work at all.
>
> > The problem at hand is simple enough - I want to create a record that
> > hold records.   For example I have (defrecord foo [list1, list2])
> > where list1 and list2 are defined records themselves.   The issue is
> > that the data in list1 and list2 is dynamic - it is loaded from a file
> > at run time.    So I do the following:
>
> > (def MyFoo (foo. nil nil))
>
> > (defn get-data [path]
> >   (let [list1Data (load-data-from-file path)]   ; fill in the record
> > for list 1
> >       (assoc MyFoo :list1 list1Data)              ; assign the data
> > record to the foo record
> >      (:list1 foo)
> > ))
>
> > As I say, if I do this non-dynamically in the REPL I get the proper
> > result.
>
> > In my program (using let)  (:list1 foo) always remains nil.    What am
> > I doing wrong?   And how can I get the fields of foo to take on the
> > dynamic data.
>
> > Bill
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with 
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to