2

I am pretty new to F# so please don't blame me :)

My service gives me User, Group and other objects. The userobject looks like this:

{
  "id": 0,
  "firstname": null,
  "lastame": null,
  "emailaddress": null,
  "active": false,
  "groups": null,
  "groupcount": 0,
  "createdgroups": null,
  "createdgroupscount": 0,
  "createdfiles": null,
  "createdfilescount": 0,
  "createdfolders": null,
  "createdfolderscount": 0,
  "createdvideos": null,
  "createdvideoscount": 0,
  "createdcategories": null,
  "createdcategoriescount": 0
}

The group object looks like this:

{
  "id": 0,
  "name": null,
  "description": null,
  "videos": null,
  "videocount": 0,
  "files": null,
  "filecount": 0,
  "members": null,
  "membercount": 0,
  "creator": {
    "id": 0,
    "firstname": null,
    "lastame": null,
    "emailaddress": null,
    "active": false,
    "groups": null,
    "groupcount": 0,
    "createdgroups": null,
    "createdgroupscount": 0,
    "createdfiles": null,
    "createdfilescount": 0,
    "createdfolders": null,
    "createdfolderscount": 0,
    "createdvideos": null,
    "createdvideoscount": 0,
    "createdcategories": null,
    "createdcategoriescount": 0
  }
}

Now my problem is, that the creator property is type of User. And the members property is a list of users.

This is how my F# types look like, the User first:

type User =
    member this.ID = 0
    member this.Firstname = ""
    member this.Lastname = ""
    member this.Emailaddress = ""
    member this.Active = false

    member this.AllGroupsCount = 0
    member this.Groups = List<Group>.Empty

    member this.AllCreatedGroupsCount = 0
    member this.CreatedGroups = List<Group>.Empty

    member this.AllCreatedFoldersCount = 0
    member this.CreatedFolders = List<Folder>.Empty

    member this.AllCreatedFilesCount = 0
    member this.CreatedFiles = List<File>.Empty

    member this.AllCreatedCategoriesCount = 0
    member this.CreatedCategories = List<Category>.Empty

    member this.AllCreatedVideosCount = 0
    member this.CreatedVideos = List<Video>.Empty

And this is the group object:

type Group =
    member this.ID = 0
    member this.Name = ""
    member this.Description = ""
    member this.Videos = List<Video>.Empty
    member this.Files = List<File>.Empty
    member this.Members = List<User>.Empty
    member this.Creator = null

Now the problem is, when I place the Group before the User object the group says User is not defined, when I put the User before the Group, it says the Group is not defined, how can I fix this?

Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
Knerd
  • 1,892
  • 3
  • 28
  • 55

1 Answers1

6

You can handle this with a recursive group:

type User =
    member this.ID = 0
    ...
and Group =
    member this.ID = 0
    ...

Note that the way you're defining your types, all the properties will have the constant values given. The natural way to define this kind of data in F# is with a record:

type User =
    {
        ID : int
        ...
    }
and Group =
    {
        ID : int
        ...
    }
Ganesh Sittampalam
  • 28,821
  • 4
  • 79
  • 98
  • Ok that helped :) But can how can I define a constructor? The point is, I need to use the library later in C#. – Knerd Dec 31 '13 at 21:31
  • 2
    If you want to use from C#, then this style of definition is recommended: http://msdn.microsoft.com/en-us/library/dd233192.aspx – Ganesh Sittampalam Dec 31 '13 at 21:47