purescript / purescript-prelude

The PureScript Prelude

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Discard instance for Void?

safareli opened this issue · comments

Motivation:

We know that it's easy to accidentally discard values in do notation for example so we have special Discard class so only Unit gets discarded.

But we still have functions like: launchAff_ ∷ ∀ a. Aff a → Effect Unit. I've seen/written similar functions in the past too.

Except some minimal performance win, only reason I see which can justified this pattern is that it works for Aff Unit and Aff Void. i.e. if we had Aff Unit → Effect Unit user would have to change Void with Unit using void or map absurd.

Solution:

To have best of both worlds is to define Discard instance for Void and then people can start using it this way ∀ a. Discard a => Aff a → Effect Unit.


Would love to hear what people thing about this.

I think a Discard instance for Void makes sense but I don't understand your reasoning; I don't see what the relevance of the argument of launchAff_ being polymorphic is.

I think a Discard instance for Void is a bad idea. You're not usually supposed to discard a value of type Void, you need it to create some a you don't have access to.

This would be especially confusing because Void is a bad name in that every C/Java programmer remembers that void == Unit in those languages, which is a value you're actually supposed to discard.

This reminds me of Conor McBride's "Perhaps Not The Answer You Were Expecting But You Asked For It" 1.2

1.2 What does () mean in Haskell?
() means “Boring”. It means the boring type which contains one thing, also boring. There is nothing interesting to be gained by comparing one element of the boring type with another, because there is nothing to learn about an element of the boring type by giving it any of your attention. It is very different from the empty type, called (by people I wish had chosen a better name like the one I suggested) in Haskell Void. The empty type is very exciting, because if somebody ever gives you a value belonging to it, you know that you are already dead and in Heaven and that anything you want is yours. But if somebody gives you a value in (), don’t get excited. Just throw it away.

I think a Discard instance for Void makes sense but I don't understand your reasoning; I don't see what the relevance of the argument of launchAff_ being polymorphic is.

So you could only launchAff_ either Aff Unit or Aff Void


argument of @kritzcreek was convincing to me, so closing.