JordanMartinez / learn-halogen

Learn purescript-halogen using a bottom-up apporach via this "clone-and-play" repository
188 stars 28 forks source link

Const Void or Const Unit for unused Query #65

Closed milesfrain closed 4 years ago

milesfrain commented 4 years ago

If queries are ignored, should QueryType be Const Void or Const Unit? Are they interchangeable? I see both types used in different places in the docs.

JordanMartinez commented 4 years ago

Void is a type that doesn't have a runtime value at all. We use that type to indicate 'this code will never run.' Unit is a type that only has one possible value, Unit. In other words, "this code can run." Const is like a placeholder Functor (e.g. when a type signature requires you to use a Functor, but you don't need/want one).

So, Const Void is the correct version because it indicates that it's impossible to ever run the query. That would be absurd, which is why we use absurd to satisfy places in the type signature where such a function is needed. If we used Const Unit, the query code can still run.

Which places are you referring to?

milesfrain commented 4 years ago

Which places are you referring to?

The blue links in the original message. They were subtle. Const Void and Const Unit

JordanMartinez commented 4 years ago

I saw that those were links, but I did not click on them. Sorry. That's on me, not you.

Yes, the Const Unit should be Const Void.

srghma commented 4 years ago

it's also possible to use forall like

-- approach is used here 
-- https://github.com/purescript-halogen/purescript-halogen/blob/bb715fe5c06ba3048f4d8b377ec842cd8cf37833/examples/higher-order-components/src/Harness.purs#L43
-- https://github.com/purescript-halogen/purescript-halogen/blob/bb715fe5c06ba3048f4d8b377ec842cd8cf37833/examples/components-inputs/src/Container.purs#L39

simpleChildComponent :: forall query input messages . H.Component HH.HTML query () input messages Aff

-- and render using `unit` for values and `absurd` or `const unit` for functions

let index = unit -- or some integer
let input = unit -- or some datatype. On first render passed to `initialState` and to `receive` on subsequent renders if `input` is changed
let messageHandler = absurd -- or `Just <<< HandlePanelMessage`
HH.slot _proxy index simpleChildComponent input messageHandler

instead of

type MyQuery = Const Void
type MyInput = Void
type MyMessages = Void
simpleChildComponent :: H.Component HH.HTML MyQuery () MyInput MyMessages Aff
JordanMartinez commented 4 years ago

@srghma Hmm... Good point.

The template and lessons serve as a way to help people get used to the types and what they mean. If they need to write something quick, but want to avoid compiler errors as much as possible, then they should use the template.

Your approach is more for those who are already familiar with Halogen. Still, I'd like to add it as a tip or something at the end of this project. We could encourage people to use that approach once they feel comfortable with Halogen.

milesfrain commented 4 years ago

help people get used to the types and what they mean

The guide is even more terse with the types:

component :: forall q i o m. H.Component HH.HTML q i o m

But I found explicit types most helpful when starting out with this repo.

srghma commented 4 years ago

@milesfrain and it's possible to misinterpret m as monad instead of messages

JordanMartinez commented 4 years ago

@milesfrain Looks like this was fixed in current development branch. I'm not sure when, but I'm going to close this issue in the next release. @srghma Could you open an issue describing your tip and adding it to this project?

Edit: It wasn't fixed in current development branch, but a replace-all didn't take long either.