Reputation: 634
I'm using StackExchange.Redis inside a small F# project and I need to cast the db.StringGetAsync() return value to an int. I've found a way to do it like this:
let! intFromRedis= async {
let! value = db.StringGetAsync(key) |> Async.AwaitTask
return int value
}
But it would be prettier to be able to do something like this instead:
// Better syntax but it does not compile
let! intFromRedis : int = db.StringGetAsync(key) |> Async.AwaitTask
Is there a better syntax for this?
Upvotes: 0
Views: 245
Reputation: 6223
As has been pointed out in the comments, technically, the question asks for Async.map
, which could be defined similar to the code in the question.
But I'd rather recommend to keep it simple. Casting on a second line would look like this:
let! redisValue = db.StringGetAsync(key) |> Async.AwaitTask
let intFromRedis = int redisValue
And I'm not even sure intFromRedis
is more readable than int redisValue
. It's barely shorter, and the latter expression is self-explanatory. Thus, there's no real problem in keeping just the first line and using int redisValue
.
Using rare or custom functions reduces readability. A cast – and maybe a value binding – are simple, short enough, and any programmer reading the code should understand what is happening.
Upvotes: 2