cats icon indicating copy to clipboard operation
cats copied to clipboard

Call-by-name parameter for `as`

Open ziggystar opened this issue 4 years ago • 4 comments

This issue in http4s shows that Functor.as (and all the paths leading there) should evaluate their argument lazily. Maybe... Please evaluate.

https://github.com/http4s/http4s/issues/4971

ziggystar avatar Jul 20 '21 09:07 ziggystar

It's very tricky to explain when "you should use map(_ => x) instead of as(x)".

joroKr21 avatar Jul 20 '21 11:07 joroKr21

Yes, this is a frustrating one.

How might we fix it in a compatible fashion? Could we overload it, making the existing one non-implicit and private? Can anyone think of instances that override the default, and if so, exploit its strictness?

rossabaker avatar Jul 31 '21 21:07 rossabaker

How might we fix it in a compatible fashion? Could we overload it, making the existing one non-implicit and private? Can anyone think of instances that override the default, and if so, exploit its strictness?

Overloading works, particularly since we can make the old one protected. We actually had to do that in Cats Effect 3 since we accidentally defined SyncIO#as to be lazy.

The overriding is… more of a problem. I'm not sure if we can fix that, since overloading would take away all of the overridden call sites.

djspiewak avatar Jul 31 '21 22:07 djspiewak

I found no overrides in Cats with GitHub search, but it's worth checking with proper tooling. Even then, there are of course instances outside Cats. Like Sync.

rossabaker avatar Aug 02 '21 04:08 rossabaker