Thursday, December 29, 2011

F# ≥ C# (Units of Measure II)

During the Christmas party, I heard a story about my friend got a ticket in Canada. The reason is simple: he drove 100 miles/hour in Ontario Canada where the speed limit is 100 km/hour. :-(

The fine for his case could be $10,000. Fortunately he did not get that scary number on his ticket, but still cost him a fortune.

When I was listening to his story, I realize how important the unit is and how nice F# provide such a feature. Other than the sample in the previous post, the following code snippet shows how a point class can have a unit with it, so that I can define a point class with "cm" unit, I believe this is very useful when doing scientific computation.

[ < Measure > ] type cm
type Point< [ < Measure > ] 't>(x:int< 't >,y:int< 't >) =
    member val X = x with get,set
    member val Y = y with get,set
    static member ( * ) (a:Point<_>,b:int) = Point(b*a.X,b*a.Y) 
let a = Point< cm >(1< cm >, 1< cm >)
a.X <- 3 < cm > 
printfn "%A" a.X
let c = a * 4

Tuesday, December 20, 2011

Use F# to Implement INotifiyCollectionChanged Interface

Today I find the INotifyCollectionChange interface on C# portable library. Because it is the core part of data binding to collection, I decide to implement this interface.

This implementation covers the following concepts, please also scroll down to see the full list.. :-)

  • declare an F# type (see color)
  • declare event (see color)
  • constructor: invoke base method (see color)
  • implement interface (see color)
  • mutable variable (see highlight comment)
  • inheritance (see highlight comment)
The other features are list at end of the page.

type ObservableList < 'T > () = 
    inherit List<'T>()     //inherit List
    let collectionChanged = Event< _ , _ > ( )

    let mutable isTriggerEvent = false   //mutable varaible

    new(items) = ObservableList<'T>() then base.AddRange(items)

    interface INotifyCollectionChanged with
        [ <  CLIEvent  > ]
        member this.CollectionChanged = collectionChanged.Publish

    member this.IsTriggerEvent
        with get() = isTriggerEvent
        and set(v) = isTriggerEvent <- v

    member this.Add(n:'T) = 
        this.TriggerAdd(n, base.Count)

    member this.Remove(n:'T) = 
        let index = this.IndexOf(n)
        if index <> -1 then
            let r = base.Remove(n)
            this.TriggerRemove(n, index)

    member this.Remove(n:'T seq) =
        n |> Seq.iter (this.Remove >> ignore)
    member this.RemoveAt(index) = 
        if this.Count > 0 then 
            if index >=0 && index < this.Count then
                let item = base.[index]
                this.TriggerRemove(item, index)
    member this.RemoveRange(index, count) = 
        [0..count-1] |> Seq.iter (fun i -> this.RemoveAt(index))

    member this.Sort()= 
    member this.Sort(comparer) = 
    member this.Sort(index, count, comparer) = 
        base.Sort(index, count, comparer)

    member this.Reverse() = 
    member this.Reverse(index, count) = 
        base.Reverse(index, count)

    member this.AddRange(items) = 
        items |> Seq.iter this.Add

    member this.Clear() = 

    member this.Insert(index, item) = 
        base.Insert(index, item)
        this.TriggerAdd(item, index)
    member this.InsertRange(index, items) = 
       items |> Seq.iteri (fun i item -> this.Insert(index+i, item))

    member this.Item
        with get(i) = base.[i]
        and set i v = 
            let old = base.[i]
            base.[i] <- v
            this.TriggerReplace(v, old, i)

    member private this.TriggerAdd(item, index) =
        if this.IsTriggerEvent then
            collectionChanged.Trigger(this, NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index))
    member private this.TriggerRemove(item, index) =
        if this.IsTriggerEvent then
            collectionChanged.Trigger(this, NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index))
    member private this.TriggerReplace(item, oldItem, index) =
        if this.IsTriggerEvent then
            collectionChanged.Trigger(this, NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, oldItem, index))
    member public this.TriggerReset() =
        if this.IsTriggerEvent then
            collectionChanged.Trigger(this, NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null, -1))

    static member FromList(items) = ObservableList(items)

  • indexed property (see color)
  • trigger event (see color)
  • static method (see color)
  • public and private member (see color)

Monday, December 19, 2011

F# ≥ C# (Meta Programming)

When C# first introduced the reflection, I was amazed by the idea writing or manipulating other programs. I can even read my own program to decide what to do next. This is something called meta programming. F# not only has reflection support but also provides more advanced meta programming technique: Quotations and ReflectedDefinition.

[ < ReflectedDefinition > ]
let myFunction a b c =

let myQuotation = < @@ myFunction @@ > 

the myQuotation quotation can be viewed as a parsing tree, from which C++ or SQL code can be generated.

The following code is a skeleton to traverse the quotation tree. You can insert your code in each active pattern. As for the debugging, quotation visualizer can be a good choice.

let rec iterate exp
match exp with
        | DerivedPatterns.Applications (e, ell) ->
        | DerivedPatterns.AndAlso (e0, e1) ->
        | DerivedPatterns.Bool e ->
        | DerivedPatterns.Byte e ->
        | DerivedPatterns.Char e ->
        | DerivedPatterns.Double e ->
        | DerivedPatterns.Int16 e->
        | DerivedPatterns.Int32 e->
        | DerivedPatterns.Int64 e ->
        | DerivedPatterns.OrElse (e0, e1)->
        | DerivedPatterns.SByte e ->
        | DerivedPatterns.Single e ->
        | DerivedPatterns.String e ->
        | DerivedPatterns.UInt16 e ->
        | DerivedPatterns.UInt32 e ->
        | DerivedPatterns.UInt64 e ->
        | DerivedPatterns.Unit e ->
        | Patterns.AddressOf address ->
        | Patterns.AddressSet (exp0, exp1) ->
        | Patterns.Application (exp0, exp1) ->
        | Patterns.Call (expOption, mi, expList)  ->
        | Patterns.Coerce (exp, t)->
        | Patterns.DefaultValue exp ->
        | Patterns.FieldGet (expOption, fi) ->
        | Patterns.FieldSet (expOption, fi, e) ->
        | Patterns.ForIntegerRangeLoop (v, e0, e1, e2) ->
        | Patterns.IfThenElse (con, exp0, exp1) ->
        | Patterns.Lambda (var,body) ->
        | Patterns.Let (var, exp0, exp1) ->
        | Patterns.LetRecursive (tupList, exp) ->
        | Patterns.NewArray (t, expList) ->
        | Patterns.NewDelegate (t, varList, exp) ->
        | Patterns.NewObject (t, expList) ->
        | Patterns.NewRecord (t, expList) ->
        | Patterns.NewObject (t, expList) ->
        | Patterns.NewRecord (t, expList) ->
        | Patterns.NewTuple expList ->
        | Patterns.NewUnionCase (t, expList) ->
        | Patterns.PropertyGet (expOption, pi, expList) ->
        | Patterns.PropertySet (expOption, pi, expList, e) ->
        | Patterns.Quote e ->
        | Patterns.Sequential (e0, e1) ->
        | Patterns.TryFinally (e0, e1) ->
        | Patterns.TryWith (e0, v0, e1, v1, e2) ->
        | Patterns.TupleGet (e, i) ->
        | Patterns.TypeTest (e, t) ->
        | Patterns.UnionCaseTest (e, ui) ->
        | Patterns.Value (obj, t) ->
        | Patterns.Var v ->
        | Patterns.VarSet (v, e) ->
        | Patterns.WhileLoop (e0, e1) ->
        | _ -> failwith "not supported pattern"
Please note that the parsing tree needs some recursive function, so the iterate function is decorated "rec".

Saturday, December 17, 2011

F# ≥ C# (Concise Syntax)

Constant customer feedback shows F#'s code is more concise, which improves their productivity, but did not give any detailed information. I was always wondering what could make F# concise.

  • Implicit constructor and parameter

Today I declare a class and suddenly realize there is no constructor defined. The constructor is defined between two brackets as shown below:

type Account(name, socialNumber) = ...

the nice part is not just saving some typing, but also the value can be referenced from the code inside type definition. I would say this design is very considerate because those constructor parameters are mostly needed in other parts of the type definition. So instead declaring a class field and expose the field as a property, F# can expose property in one shoot.

type Account(name, socialNumber) =
    member me.Name with get() = name               //expose parameter
    member me.SSN with get() = socialNumber
    member me.Print() = printfn "%s - %d" name socialNumber   // access parameter from constructor

  • this keyword

If you take a closer look at the code above, you might scratch your head wondering what "me" is. Is this VB? No. In F#, you can use any identifier as "this" pointer. In the code above, I use "me", I saw some code use "x". For people from other background, such as VB, class definition is more friendly.

Do forget the "Automatic Generalization" feature, it can save many type definition code or even no need for "var".

Facing a long list of tasks when you open your computer in the morning, are you thinking about improve your productivity and can go home earlier?  :-)

Friday, December 16, 2011

C# type and F# representation

Today I got a question about how to map the following structure to F#. Nothing fancy, but it might help somebody transfer C# skill to F#.

namespace OuterNamespace
{    public sealed class PublicSealedClass { }
     internal sealed class FriendSealedClass { }
     public abstract class PublicAbstractClass { }
     internal abstract class FriendAbstractClass { }
     public class PublicClass { }
     internal class FriendClass { }
     public interface PublicInterface { }
     internal interface FriendInterface { }
     public struct PublicStructure { }
     internal struct FriendStructure { }
     public enum PublicEnum { }
     internal enum FriendEnum { }

F# code:

namespace OuterNamespace

[ < Sealed > ]
type PublicSealedClass() = class end

[ < Sealed > ]
type internal FriendSealedClass() = class end

[ < Sealed > ]
type PublicAbstractClass () = class end

[ < AbstractClass > ]
type internal FriendAbstractClass  () = class end

type PublicClass () = class end
type internal FriendClass () = class end

type PublicInterface = interface end
type internal FriendInterface = interface end

[ < StructAttribute > ]
type PublicStructure = struct end

[ < StructAttribute > ]
type internal FriendStructure  = struct end

//we do not support empty enum, so must have something inside.
type PublicEnum =
    Element = 0

type internal FriendEnum  =
    Element = 0

Monday, December 12, 2011

Set break point in a F# function

Not quite sure if this is a big deal, but I always frustrated about how to set a break point in a F# or C# LINQ expression. The mouse click does not always work, but today, but today the keyboard saved me. So

  1. move mouse to the function definition
  2. press "F9"

put a smile on my face..  :-)

Sunday, December 11, 2011

F# ≥ C# (Object Expression)

It has been a long week working on various F# libraries. This blog shows how to use Object Expression to make your code more concise.

I was working on a UI project which involves WPF/Metro command binding. The ICommand interface requires two functions: CanExecute and Execute. Because I already have some functions implemented in the F# project, I'd image implement ICommand in F# project is much easier.

In the WPF converter code snippets, I defined a new type to represent the converter. The code is like:

type StringToVisiblityConverter() =
    inherit ConverterBase(stringToInt>>intToBool>>boolToVisibility |> convert, nullFunction)

This technique is very familiar to C# developer. But I do not like the way the code, especially the inherit ConverterBase. What I really want to say is: create a command with two functions. I ended up with the code like the following:

let createCommand action canExecute=
            let event1 = Event<_, _>()
                new ICommand with
                    member this.CanExecute(obj) = canExecute(obj)
                    member this.Execute(obj) = action(obj)
                    member this.add_CanExecuteChanged(handler) = event1.Publish.AddHandler(handler)
                    member this.remove_CanExecuteChanged(handler) = event1.Publish.AddHandler(handler)

     let myCommand = createCommand
                        (fun _ -> ())
                        (fun _ -> true)
createCommand is a template function which takes two functions as input parameter. This function returns a ICommand object. The myCommand takes two dummy functions and generate a concrete object. I can reference this myCommand object from C# without any problem and the code is more readable than C# version.

As a professional developer, I am interested any technique can make my code more readable. How about you?  :-)

Thursday, December 8, 2011

F# Seattle Group created!

An F# Meetup Group in Seattle has now been launched by Tao Liu on the F# team. (It's only just been created, so get in early!)

Description: Description:

If you're in the Seattle or King County area and are interested in F#, then please join, and help get the group off to a great start.
A good meetup group absolutely depends on people participating by giving good presentations, and also on getting F# presenters who are coming through town to come by and give a talk. So start considering what talks you'd like to give, and what other activities you'd like to see the group doing. The meetup group site is a great place to propose new activities, topics and speakers.

There are also other F# meetup groups in New YorkLondon as well as other related F# or functional or .NET meetup groups

Saturday, December 3, 2011

Null character in F#

Unlike C#, null character can be represent as '\0'. in F#, you have to type more zero's.. :)

follow the spec:

let a = '\0' won't compile, but
let a = '\000' will.

Friday, December 2, 2011

F# ≥ C# (Units of Measure)

If you involves in any real world application, the number most likely has a special unit. Adding this unit not only make sure the code is more readable, but also decrease the chance to introduce a bug.

The unit is a compile time feature, so there is no performance hit by using unit.

The simple unit sample will be:
[ < Measure >  ]type litre
[ < Measure > ]type pint
let MeasureSample1() =
   let v1 = 2.
   let v2 = 1.
   let ratio =  1.0 / 1.76056338
   let pintToLitre pints =
       pints * ratio
the v1 and v2 are no longer simple float in the eyes of F# compiler. It is a special value with a unit. More complex samples will be something like the following code snippet.

The unit can not only decorate the simple type like float or int, but also decorate the complex type. The unit in the following code snippet is serving as a parameter to create new types: one is USD bank account and the other is CAD account. Can you easily find something like this in C#?

type AccountState =
   | Overdrawn
   | Silver
   | Gold
[ < Measure >  ] type USD
[ < Measure >  ] type CND
type Account <  [ < Measure > ]  'u > ( ) =
   let mutable balance = 0.0<_>
   member this.State
       with get() =
           match balance with
           | _ when balance <= 0.0<_> -> Overdrawn
           | _ when balance > 0.0<_> && balance < 10000.0<_> -> Silver
           | _ -> Gold
   member this.PayInterest() =
       let interest =
           match this.State with
               | Overdrawn -> 0.
               | Silver -> 0.01
               | Gold -> 0.02
       interest * balance
   member this.Deposit(x:float<_>) =  balance <- balance + x
   member this.Withdraw(x:float<_>) = balance <- balance - x
let MeasureSample4() =
   let account = Account < USD >()
   account.Deposit(LanguagePrimitives.FloatWithMeasure 10000.)
   printfn "us interest = %A" (account.PayInterest())
   account.Withdraw(LanguagePrimitives.FloatWithMeasure 20000.)
   printfn "us interest = %A" (account.PayInterest())
   let canadaAccount = Account < CND >()
   canadaAccount.Deposit(LanguagePrimitives.FloatWithMeasure 10000.)
   canadaAccount.Withdraw(LanguagePrimitives.FloatWithMeasure 500.)
   printfn "canadian interest = %A" (canadaAccount.PayInterest())

Thursday, December 1, 2011

Type infer in LINQ

If you really want to use LINQ in F#, sometimes you have to specify the type:

counts is a Dictionary
        .OrderByDescending(fun x -> x.Value)
        .Select(fun x -> x.Key)   //change this line to the line below will make you compile

.Select(fun (x:KeyValuePair<_,_>) -> x.Key).  

F# ≥ C# (Extend type by properties)

In previous post, the type extension give me a lead to today's blog. C# programmer has been asking for "extension property" since introducing "extension method". Do not wait for C# any more, F# already got the feature:

type SomeClass1(x: int, y: float) =
   new() = SomeClass1(0, 0.)
type SomeClass1 with
    member this.PropertyA
        with get() = "aa"
        and set (v) = ....

this feature is not just for F#. You can invoke this PropertyA from C# as well. Is F# more convenient? :)

Sunday, November 27, 2011

F# ≥ C# (Automatic Generalization)

In previous blog post, I use the Chain of Responsibility in a real world example. Five minutes later, I realize F# provides a very nice type generalization which makes my coding much easier than using C#.
let chainTemplate processFunction canContinue s =
    if canContinue s then
        processFunction s
    else s
the input parameter s is generalized so it is just a place holder. In my simple design pattern sample, the type is an integer.
// simple design pattern sample
let canContinueF _ = true
let processF x = x + 1
In the real world sample, s is actually a tuple type.

// real world problem 
let canContinueF (v,unitNameIndex) = v >= unit && unitNameIndex < unitNames.Length-1
let processF (x, unitNameIndex) = (x / unit, unitNameIndex+1)

F#'s automatic generalization feature almost makes my template pattern retired.

If you want to implement the same feature in C#, seems that you have to use generic function. It is true that C# can do the same thing, but readability and productivity will be much lower than the F# version. 

Sample in detail - Chain of Responsibility pattern

this post is to use the Chain of Responsibility pattern to implement a simple function to show the byte in KB, MB, or GB.

In this post, I use the Chain of Responsibility pattern to replace the while loop. The loop itself is done by the composite function defined below. There is another version to composite functions but I found it not easy to debug.

The process will stop when the canContinueF returns false. The result value won't be processed and just slip through other function. The F# provides a good generalization so I do not have to refactor any design pattern code.

let chainTemplate processFunction canContinue s =
    if canContinue s then
        processFunction s
    else s
let composite f a n =
    [1..n] |> (fun _ -> f) |> Seq.fold (fun acc n -> acc |> n) a
let getReadableSize value =
    let unitNames = ["B"; "KB"; "MB"; "GB"; "TB"]
    let unit = 1000.
    let canContinueF (v,unitNameIndex) = v >= unit && unitNameIndex < unitNames.Length-1
    let processF (x, unitNameIndex) = (x / unit, unitNameIndex+1)
    let chainFunction = chainTemplate processF canContinueF   //combine two functions to get a chainFunction
    let (v,i) = composite chainFunction value unitNames.Length //value |> chainFunction |> chainFunction
    (v, unitNames.[i]) 
let (v,name) = getReadableSize (1001000000000000., 0)
printfn "%f %s" (System.Math.Round(v, 2)) name

Saturday, November 26, 2011

F# ≥ C# (Type extension and GI function)

At previous post, a way to expand class hierarchy and add base class. This post continues to explore the way to expand current system without touching existing code and shows how F# can do the same job with less effort. This post will show who to expand the current type by adding new methods. Also we present a special function can access type without a common base class or interface.

unlike C#'s extension methods, F#'s type extension methods just like the methods on the type. Combining the power from Generic Invoke (GI) function, we can expand current system much easier. The following is the code:
  • the feed function defined at first line is the GI function, it takes any time with a member Feed with signature unit->unit.
  • the original type does not have Feed function. The feed function is from type extension. 
Hopefully from this post and previous post, we have an end-to-end story about how to expand existing system with minimum effort and eventually shows how F# ≥ C#. Please note that CatType and DogType are something not changeable.

// the catType and dogType is from third party and can't change it. 
type CatType =
    | CatA of string
    | CatB of string
    | CatC of string * int
type DogType =
    | DogA of string
    | DogB of string
    | DogC of string * DogType
// code to extension 

let inline feed (x : ^T) = (^T : (member Feed : unit -> unit) (x))
type Animal =
    | Cat of CatType
    | Dog of DogType
// type extension to add a new method: Feed 
type DogType with
    member this.Feed() = printfn "feed dog"
type CatType with
    member this.Feed() = printfn "feed cat"
type Animal with
    member this.Feed() =
        match this with
        | Dog(m) -> feed m
        | Cat(m) -> feed m
let dogs = [ DogA("a"); DogB("a1") ] |> Animal.Dog
let cats = [ CatA("b"); CatB("b1") ] |> Animal.Cat
let zoo = dogs @ cats
zoo |> Seq.iter feed

From the previous post, we have the ability to unify two type into a single list structure, which provide the foundation to process them in a single run.

F# ≥ C# (Discriminated Unions)

It was a boring Black Friday with no interesting discount, so I decide to sit down and continue my blog serials about why F# is better than C#.

I came across my colleague's blog about discriminated union in F#. He proposed an interesting idea to use discriminated unions to do a class hierarchy. I really like this idea but the conclusion in his blog disappoints me a little bit.

Whereas a class hierarchy is "open", in that someone else can come along later and add a new subclass, discriminated unions are "closed", in that the author of the type specifies all the alternatives once-and-for-all (analogy: imagine a "sealed" class hierarchy).  This is often the main factor to consider when trying to decide between a class hierarchy and a discriminated union.

I like the elegant discriminated union. Combining with record, it should be more powerful and concise than C#'s class-based design. If the type hierarchy is closed, that will impose a big problem. Making two different types' data into a single list will be very difficult, this problem increases coding effort. For example, if we have CatType and DogType defined as below:

type CatType =
    | CatA of string
    | CatB of string
    | CatC of string * int
type DogType =
    | DogA of string
    | DogB of string
    | DogC of string * int 

let dogs = [ DogA("a"); DogB("a1") ]
let cats = [ CatA("b"); CatB("b1") ]

if we cannot make a new list which can hold dogs and cats, we have to explicitly access the "dogs" and "cats" to apply certain function. I have to make the discriminated union hierarchy "open" and shows how F# is better than C#.

So my problem becomes to find a way to combine these two list into a new list, something like:

let pets = dogs @ cats

in order to make this happen, I introduce a more general discriminated union type.

type Animal =
    | Cat of CatType
    | Dog of DogType
and convert the dogs and cats list to 

let dogs = [ DogA("a"); DogB("a1") ] |> Animal.Doglet cats = [ CatA("b"); CatB("b1") ] |> Animal.Cat

if you compile the code above, you can get a pets whose type is Animal. Good! Now I can show F# = C#. But how about the greater (>) part? 

In C#, in order to make two incompatible types be stored in list, we have to make them have a common base class or interface. If you can change two types' source code, that won't be a big problem. Adding some interfaces to CatType and DogType only makes your code several lines more. In a big team, you will have to ask around to get permission from code owner(s). 

If CatType and DogType is from a third party library, I believe it will be nightmare. F#'s implementation keeps the original implementation untouched and expands (generalizes) the system in a much cleaner way. Is that more productive?

Saturday, November 19, 2011

F# ≥ C# (Record)

Continue previous post, let us continue to explore how easily F# can reduce your coding effort compare to C#.

Until recently I started to notice the record type in F#. Originally I thought it was like C#'s struct or class, but it is not true.

type myPointRecord = { X : float; Y::float }
let pointA = { X = 0; Y= 0 }
let pointB = { X = 0; Y = 0 }

Two points are actually same. The way to prove they are same are very simple and intuitive:

let isEqual = pointA = pointB

The biggest advantage for a record is you can compare its content with "=" operator. I tried to compare two C# structs with "==", but it does not work. It is option to override C#'s equals and GetHashCode function to make a class to support non-reference comparison, but that is a lot of work especially when you are trying to make all your class to support this kind of content comparison.

F# ≥ C# (Tuple and Swap)

F#, as a new language, has something C# cannot easily do. That's what I really love F#, which makes the concise and less bug. Let me give some samples about how F# can do better than C#. Today is the first post for swap and tuple

swap is the simplest operation I could think of. Now I want a general swap function can handle any type. For C# version, object type or generic function can be used. But no matter what,  a temporary variable is needed. Now F# can do something simpler:

          let swap(a,b) = (b,a)
the passing parameter is (a,b) and the return value is (b,a). Is that simple?!

One thing always confused me is that (a,b) is a special type called tuple. So the function swap actually takes a single parameter whose type is tuple.

The tuple support more than 2 parameters, so you can do something like:

       let swap2(a,b,c,d,e) = (a,c,b,e,d)

to swap any element in a tuple in a way you prefer.

Friday, November 11, 2011

Reference DLL (mark-of-the-web)

Back from vacation which is always too short.. :-)

from F# Interactive, if there is an error like:

error FS0193: Could not load file or assembly XYZ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

The DLL has mark-of-the-web attribute set, the following link here is how to remove the attribute.

Saturday, October 22, 2011

F# and WP7 app

Let us add another sample to use F# on WP7 application, although WP still improvement.

Where Am I

On your way home, business trip, hiking route, one click send your location on your way home, business trip, hiking route. Keep contact with the one you care.

Features include:

- find current longtitude and latitdue
- find civic address
- find current speed
- send location information by email
- send location information by SMS


Tuesday, October 18, 2011

unbox and data conversion

As a C# programmer, I was expect something like

object a = 123;
ulong b = (ulong)a;

should also work on F#. in order to make this work on F#, you need another operator "unbox"

let (a:int) = 123;
let (b:obj) = box(a)   //make the b as an object
let c = uint64(unbox(b)) //unbox b first and then you can convert