Anonymous Types in F#?

So, at the end, how did you solve this problem?

I was also struggling with arrays and data grids too, but I haven't yet found out how to get it working.

If any of you can give pointers or examples on using data grids with in memory sequence structures like arrays or lists, it would be of great help to me.

Thanks in advance.

By on 7/11/2008 9:23 AM ()

Hi, there. Are you looking for a way to bind data source to data grid with change notification? If so, and you are building your data source with LINQ, then CLINQ may become a consideration. Check it out at the following url: .

Hope that helps.

By on 7/11/2008 6:56 PM ()

The simplest way is just to dump everything into an System.ComponentModel.BindingList<T> when you've done processing. This takes care of displaying the data and the grid will respond to updates.

Cheers,
Rob

By on 7/11/2008 11:16 AM ()

The problem here is that the data grid view does not support IEnumberable directly, a collection must implement at least IList, or at least that's what MSDN says:

I suspect the C# does some slight of hand so that some collections that are generated via LINQ also support the IList interface as well as IEnumerable interface.

Concerning column name headers in the grid, I'm not quite sure what you want. In F# tuples are type where the fields have no name and are accessed by order, records are types where the fields have explicit names. Tuples work in a data grid but have column headings Item1, Item2 etc. If you want your column headings to have names use records, this works quite nicely. I know records are slightly harder to define that a C# annonymous type but there still considerably less work to define that a normal C# type.

Cheers,
Rob

By on 7/11/2008 6:13 AM ()

Hi, Rob. First I need to correct something. I created a Windows application in C# just now and bound a query expression to a DataGridView on the form. This time it showed nothing just as I did in F#. I cannot recall what I had done to make it display in the past. Anyway, let's go back to the main question.

Is there a way to define anonymous type in F# as we can in C# like the following (According to your response, I know I made the question too complicated; therefore let's shorten it to one question.):

1
let b = { Title = "Foundations of F#"; Authors = "Robert Pickering" }

It's similar to a record type except that I did not explicit define such a record type before I use, and the compiler does the trick behind the scene when it sees this line of code. Will it be possible to do similar thing in F#?

Thanks in advance!

By on 7/11/2008 7:09 AM ()

No, F# does not support anonymous types in the same way as C#.

Tuples are similar in many ways, but have a slight different design aim. Tuples are meant to be a convient way for a programmer to return more that one item from a function call. C# anonymous types share this design aim, but more weakly since they can't be passed out of a method, unless you want to use reflection to retrive thier values as is the case with the data grid view.

Would C# like anonymous methods be a useful addition to F#? I don't know. At the end of the day records are very similar, but I'll agree there is some additional cost to declaring the record up front.

Cheers,
Rob

By on 7/11/2008 7:51 AM ()

Hi there,

Yes, currently you must define a record type. The interaction with the DataGrid is a case where anonymous types are useful.

Thanks

Don

By on 7/11/2008 6:53 PM ()

Thanks, Rob. What I want, in fact, is kind of a combination of tuple and record. Tuple is flexible when you don't know what to define in advance, like anonymous type in C#; while record brings additional member information but a formal definition before use is required. Therefore, I just wonder if there is/will be a chance to have a combination of both benefits.

Thanks for your inspiring!

By on 7/11/2008 6:39 PM ()
IntelliFactory Offices Copyright (c) 2024 IntelliFactory. All rights reserved.
| | | Trainings | | |
Built with