Hey, is it possible to write one function that accepts either a Str or a Num and does different things to them... without requiring tag prefixing?
No
Would need a tag
Hmmm darn
At least i can't think of a way.
Wouldn't expect a way to exist
What are you trying to do?
I'm trying to write a pretty printing library that can accept arbitrary data and converts it to a Str, similar to JS's JSON.stringify
It works for Str and Num if the user provides a prefix tag to indicate which it is
But as soon as you want to stringify a List of strings or numbers... or a Record of anything... The user would have to describe the entire type structure... Which defeats the purpose of a lightweight pretty printer
Is a generic Roc pretty printer impossible?
Yes, trough abilities, aka simplified type classes aka better than interfaces :)
But they are not implemented yet
Could I please see the design doc for abilities? For both library planning and excited curiosity :)
This was Richard's document, for sure we should organize this knowledge better, but it's still open issue so no sweat.
https://docs.google.com/document/d/1kUh53p1Du3fWP_jZp-sdqwb5C9DuS43YJwXHg1NzETY/edit
This is conversation thread about them:
https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/Abilities
I want to highlight the last section of that Google doc in particular - my biggest worry about abilities, by far, is that they'll become a source of unnecessary complexity by way of being used for things that had simpler solutions which didn't involve abilities :sweat_smile:
Ah, basically OOP leaking in?
Great doc, and it sounds like there won't be as strong of a need for a stringify library in the future, thanks to the power of Encode.str
!
Maybe nonstandard pretty printing will still have its use cases, but I was primarily craving that easy debug logging that JS has, as you mention in the doc
Will function definitions be able to use where
/has
selectors at runtime, similarly to when
/is
selectors? That might be very helpful.
I assume not, since this abilities sounds like a type-level feature and the doc doesn't really reference introspection
Last updated: Jul 05 2025 at 12:14 UTC