A little bit of splitting things up and using parentheses would help you a lot here:
let download (url : string) =
use webClient = new WebClient()
try
webClient.DownloadString(url)
with
| :? WebException as ex when (ex.Response :? HttpWebResponse) ->
use response = ex.Response :?> HttpWebResponse
use data = response.GetResponseStream()
use reader = new StreamReader(data)
let text = reader.ReadToEnd()
failwith (response.StatusCode.ToString() + ": " + text)
This function compiles and works as expected.
I'm not an expert, but if I had to guess I'd say that the expression after when
has to be in brackets to enforce the correct order of evaluation (so type check first, then when
it), not strictly left-to-right.
Same thing goes for failwith
, for the string concatenation to be evaluated first it should be in brackets.