views:

118

answers:

1
+2  Q: 

F# Async problem.

Hi, I've written a dummy http server as an exercise in F#.

I'm using Mono 2.4.4 on Ubuntu 10.04 x86_64, with MonoDevelop.

The following code fails to compile with the error:

Error FS0039: The field, constructor or member 'Spawn' is not defined (FS0039)

Could someone try this in VisualStudio please, I don't know whether this is a Mono problem, or my problem.

I have tried several Async examples from the F# book, and they also all produce similar messages about Async.* methods.

Thanks,

Chris.

#light

open System
open System.IO
open System.Threading
open System.Net
open System.Net.Sockets
open Microsoft.FSharp.Control.CommonExtensions

printfn "%s" "Hello World!"

let headers = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 37\r\nDate: Sun, 13 Jun 2010 05:30:00 GMT\r\nServer: FSC/0.0.1\r\n\r\n")
let content = System.Text.Encoding.ASCII.GetBytes("<html><body>Hello World</body></html>")

let serveAsync (client : TcpClient) =
    async { let out = client.GetStream()
            do! out.AsyncWrite(headers)
            do! Async.Sleep 3000
            do! out.AsyncWrite(content)
            do out.Close()
            }

let http_server (ip, port) = 
    let server = new TcpListener(IPAddress.Parse(ip),port)
    server.Start()
    while true do 
        let client = server.AcceptTcpClient()
        printfn "new client"
        Async.Spawn (serveAsync client)

http_server ("0.0.0.0", 1234)
+5  A: 

Spawn is now called Start (the library APIs have changed a bit since a couple of the books were published a few years ago).

Check the docs at

http://msdn.microsoft.com/en-us/library/ee370232.aspx

Brian
That fixed it, thanks for the link.
chrisdew