[Haskell-cafe] Ensuring that blank-state has been handled in UIs?

MarLinn monkleyon at gmail.com
Sun Apr 16 15:05:08 UTC 2017


On 2017-04-16 13:51, Saurabh Nanda wrote:
> Can we use the type-system (or anything else) to enforce the dev to at 
> least _think_ about the blank state?

That is an interesting problem. But I'd say the solution depends highly 
on your architecture. Tomas' suggestion to use NonEmpty could be one 
solution, and one you could try to build upon to track more details in 
the types. Alternatively, you could add a typeclass that just handles 
the zero-element-case. That way your constraints remind the developer to 
at least copy-paste a default implementation.

	showItemTable :: (HasZeroItemPresentation (presenter item)) => presenter item -> [item] -> Table item

But maybe there's a much simpler solution: Create a datatype that stores 
the normal presentation function, a zero-case presentation function, and 
optionally a one-item presentation function. Now whenever someone 
creates a new type of item, they need a new presentation adapter.

	data TablePresenter item = TablePresenter { showZeroItems :: Widget; showItem :: item -> Widget; showSingleItem :: Maybe (item -> Widget) }

	showItemTable :: TablePresenter item -> [item] -> Table item

This should offer safety and code reuse, and it should be easy to 
integrate because you don't even have to start with types. Why be fancy 
when the simple solutions work?

Cheers,
MarLinn


More information about the Haskell-Cafe mailing list