> type FamilyMember = {Name: string; relation: string};;
type FamilyMember = {Name: string; relation: string;}There are two different ways to create record. The first way:
> {Name = "Nate";relation = "Father"};;
val it : FamilyMember = {Name = "Nate"; relation = "Father";}Or a more explicit syntax:
> {new FamilyMember with Name = "Kelly" and relation = "Mother"};;
val it : FamilyMember = {Name = "Kelly"; relation = "Mother";}F# makes it easy for you to access the record labels:
> let f = {new FamilyMember with Name = "Nate" and relation = "father"};;
val f : FamilyMember
> f.Name;;
val it : string = "Nate"Records can be cloned, and like everything else with F#, it’s amazingly simple!
> let d1 = {new FamilyMember with Name = "reilly" and relation = "daughter"};;
val d1 : FamilyMember
> let d2 = {d1 with Name = "Cora"};;
val d2 : FamilyMember
> d2;;
val it : FamilyMember = {Name = "Cora";
relation = "daughter";}Records can be results of functions as well. Take the following type, which has a count of files and a count of directories:
type FilesAndDirectorys = {Files: int; Directories: int;}We can use it for a function like so:
let GetFilesAndDirectories loc =
let dirs = Directory.GetDirectories(loc)
let files = Directory.GetFiles(loc)
let results = {new FilesAndDirectorys with Files = files.Length and Directories = dirs.Length}
results
Next call the function with the path you want to search
let counts = GetFilesAndDirectories @"C:\" The identifier counts now contains the following data:There is another type, discriminated union types, but I'll save those for another blog entry.val it : FilesAndDirectorys = {Files = 9; Directories = 18;}
No comments:
Post a Comment