I have encountered strange issue with lists in F#.
I am using XamlXmlReader to generate list and sequence like:
let xaml_reader = XamlXmlReader(filename)
let nodes_seqs = seq { while xaml_reader.Read() do yield xaml_reader }
This code is working perfectly but if I do the same with list:
let xaml_reader2 = XamlXmlReader(filename)
let nodes_list = [ while xaml_reader2.Read() do yield xaml_reader2 ]
The result list is not correct i.e. head element of the sequence contains correct NodeType, LinePosition, LineNumber etc but head element of the list is kind of garbage with pointing to another place in XAML. Example:
let head_of_seq = Seq.head nodes_seq
let head_of_list = List.head nodes_list
After I have:
- head_of_seq.NodeType = NamespaceDeclaration
- head_of_seq.LineNumber = 2
- head_of_seq.LinePosition = 2
But:
- head_of_list.NodeType = None
- head_of_list.LineNumber = 111
- head_of_list.LinePosition = 30
XAML file itself is correct and generated by Microsoft Bend. Any clues? I’ll appreciate any help.
Why are you yielding the
XamlXmlReaderitself from within your sequence or list? That is, shouldn’t you be usingyield xaml_reader.Valueinstead?In either case, you just end up with a sequence (or list) containing the same
XamlXmlReaderinstance many times. When you iterate over the sequence once, it will appear to be correct, since the reader is being advanced once as each element is read. However, even your sequence is almost certainly not doing what you think. For instance, what happens if you try this?Because the
XamlXmlReaderis being advanced each time the sequence is accessed,first2will appear to be pointing at the second element (and looking atfirstafterfirst2has been evaluated will show the same thing).