Microsoft and Xamarin, Google and Swift

Xamarin

A few months ago I was looking at mobile app development again and so had a look at Xamarin, a cross-platform app toolset built around .NET. It seemed interesting, but it was hugely expensive. Something like $600 per year per license. When Android Studio is free and XCode is either free or $99, it was cheaper to go native.

Microsoft Buys Xamarin

Now Microsoft has acquired Xamarin and it will be free to use. So now you can theoretically write C# code and have it build (with some additional platform-specific effort) Android and iOS (and Windows Phone, if anyone cares) apps cheaply.

Google needs a new first-class language… Swift?

With Oracle constantly trying to squeeze money out of Google for their use of Java on Android, Google very obviously needs a new first-class language for Android – one that can eventually replace Java completely. Reportedly they’re considering using Apple’s Swift. Although I’ve spent only a little time with Swift, it seems like an excellent language. Like F#, it’s a hybrid functional language with objects.

Write once… in C# or Swift?

Xamarin’s supposed advantage is that you can write in one language, C#, and compile to iOS or Android. What if Swift becomes the native language for both platforms? Someone will create libraries to cross-compile the UI elements. What need is there then for Xamarin? Swift is a more modern language. Though Xamarin theoretically supports all .NET languages and thus would support F#, we know F# is a second-class citizen. Xamarin would be solely for those .NET developers who are unable to move on to new languages. If Google does adopt Swift for Android, Xamarin will become the mobile equivalent of WebForms.

F# and Swift

I’m looking at Apple’s Swift language and it’s interesting to see how similar it is to F# (which itself is similar to Scala and Haskell). I think this is all in reaction to the trouble we’re having in general with pure object-oriented programming and the advantages of a functional (or at least hybrid) approach in the age of distributed systems.

Value declaration

This is some F# for assigning variables:

let someConstant = 10
let mutable someVariable = 1.23
let specifyTypeExplicitly : String = "blah"

and in Swift:

let someConstant = 10 
var someVariable = 1.23
var specifyTypeExplicitly : Double = "blah"

let mutable for var, otherwise the same. Types are generally inferred but can be set explicitly.

Both languages have tuples and dictionaries assigned in basically the same way.

Pattern matching

Instead of F#’s match, Swift puts its matching inside a traditional switch statement.

//let rgba = ( 1.0, 1.0, 1.0, 1.0 ) "white"
//let rgba = ( 0.4, 0.4, 0.4, 1.0 ) "gray"
//let rgba = ( 0.0, 0.6, 0.8, 1.0 ) "blue is 0.8"
let rgba = ( 0.0, 0.6, 0.8, 1.0 )
switch rgba {
case ( 1.0, 1.0, 1.0, 1.0 ):
    print( "white" )
case let ( r, g, b, 1.0) where r==g && g==b:
    print("gray")
case (0.0, 0.5...1.0, let b, _):
    print("blue is \(b)")
default:
    break
}

In F#, this would be something like (this is from memory):

//let rgba = ( 1.0, 1.0, 1.0, 1.0 ) "white"
//let rgba = ( 0.4, 0.4, 0.4, 1.0 ) "gray"
//let rgba = ( 0.0, 0.6, 0.8, 1.0 ) "blue is 0.800000"
let rgba = ( 0.0, 0.6, 0.8, 1.0 )
match rgba with
| ( 1.0, 1.0, 1.0, 1.0 ) ->
    printfn "white"
| ( r, g, b, 1.0 ) when ( r = g ) && ( g = b ) ->
    printfn "gray"
| ( 0.0, g, b, _ ) when ( g >= 0.5 ) && ( g <= 1.0 ) ->
    printfn "blue is %f" b 
| _ -> ()

Some and None

Swift and F# both support the Some and None keywords for optional values. e.g.

var x : Int? = nil
switch x {
case .Some( let value ):
    print("x has a value")
case .None:

In F#:

let x : int option = None
match x with
| Some(value) ->
    printfn "x has a value"
| None ->
    printfn "x is nil"

Currying

In my first look at the language, currying doesn’t seem quite as nice.
Here’s some F#:

let add a b = a + b
let add2 = add 2
let result = add2 3 // 5

In Swift (in the tersest way I know how at present) :

let add : (Int,Int) -> Int = { $0 + $1 }
let add2 = { add( 2, $0 ) }
let result = add2( 3 ) // 5

Another way (according to this) is to make the add function return a function like in the example below, but then doing a call to the initial function does not look like a standard function call:

func add (a:Int)(_ b:Int)-> Int {
    return a + b
}
add(2)(3) // not add(2,3) // 5
let add2 = add(2)
add2(3) // 5

Conclusion

This is a very shallow comparison. I just thought it was interesting.