Pages

Wednesday, August 31, 2011

Create generic type using F#

you can use typeof operator to get a concrete type, just like C#'s typeof operator. But when thing comes to the generic type, it becomes different.

in C#, you can do: typeof(List<>), but in F#, if you do typeof> you will get List type. The solution is simple: you have to use typedefof>,this will give a generic type definition and you can use GenerateGenericType to create the concrete type. In summary: typeof: for get concrete type typedefof: get the generic type definition.

Sunday, August 21, 2011

F# quotation and expression tree

If have a function like "sample", how to write out the expression with F# quotation? I have been struggle this for whole night.


[ < ReflectedDefinition > ]
let sample (x) =
    printfn "###%A" x

It seems that we can only get the function parameters, and the methodInfo stops the expression traversing. Here is a way to solve this problem:

match expression with

| Patterns.Call (expOption, mi, expList)  ->
        matchExp expOption
        match mi with
            | DerivedPatterns.MethodWithReflectedDefinition n -> recursive call to access n
            | _ -> print mi

Saturday, August 20, 2011

F# format for Other .NET invoke

Sometimes I need to define a library let other .NET library and recognize or invoke. It must be like C# format:

namespace AAA
{
     class BB
     {
           some functions here.
     }
}

It took me one day to figure the equivalent format:

namespace AA

type BB () =
     some functions

module Program
     let b = BB()
   


F# and Design patterns

When you bet your career to a technology on F# and following Microsoft for so many years, I really do not want F# to stay like a niche language. This is the first post for several design patterns on the f# snippets web site. Hopefully more people will apply F# into more commercial software system.


I will put some description and how I think about using these pattern and F# design a software system in future post.

Tuesday, August 9, 2011

F# basic data types and printfn

do not want to look up those symbols every time:


let int = 42
let string = "This is a string"
let char = 'c'
let bool = true
let bytearray = "This is a byte string"B
let hexint = 0x34
let octalint = 0o42
let binaryinteger = 0b101010
let signedbyte = 68y
let unsignedbyte = 102uy
let smallint = 16s
let smalluint = 16us
let integer = 345l
let usignedint = 345ul
let nativeint = 765n
let unsignednativeint = 765un
let long = 12345678912345L
let unsignedlong = 12345678912345UL
let float32 = 42.8F
let float = 42.8

printfn "int = %d or %A" int int
printfn "string = %s or %A" string string  //nice we have double quote around the string when use %A
printfn "char = %c or %A" char char  //nice we have single quote around the character when use %A
printfn "bool = %b or %A" bool bool
printfn "bytearray = %A" bytearray
printfn "hex int = %x or %A" hexint hexint
printfn "HEX INT = %X or %A" hexint hexint
printfn "oct int = %o or %A" octalint octalint
printfn "bin int = %d or %A" binaryinteger binaryinteger
printfn "signed byte = %A" signedbyte
printfn "unsigned byte = %A" unsignedbyte
printfn "small int = %A" smallint
printfn "small uint = %A" smalluint
printfn "int = %i or %A" integer integer
printfn "uint = %i or %A" usignedint usignedint
printfn "native int = %A" nativeint
printfn "unsigned native int = %A" unsignednativeint
printfn "long = %d or %A" long long
printfn "unsigned long = %A" unsignedlong
printfn "float = %f or %A" float32 float32
printfn "double = %f or %A" float float