JosiahParry / quarto-site

attempting to build a new site using quarto
2 stars 2 forks source link

posts/2023-11-10-enums-in-r/ #8

Open utterances-bot opened 7 months ago

utterances-bot commented 7 months ago

Josiah Parry - Enums in R: towards type safe R

https://josiahparry.com/posts/2023-11-10-enums-in-r/

jonocarroll commented 7 months ago

This is great! I've been thinking about enums in R since I noticed the mention in the original R paper (the phrasing nearly blew my mind)... "[We] have not yet implemented a complex type, but we did include an enum type for the representation of categorical variables" -- factor is an enum (!) just not nearly as useful as the Rust one because referring to one level is just a string, so they're hardly ever used for that. Using an S7 class to implement these properly looks like a really promising idea.

JosiahParry commented 7 months ago

@jonocarroll yes! @cgmossa actually brought this up yesterday. And i find it quite interesting that wikipedia even calls out factors as a type of enumeration https://en.wikipedia.org/wiki/Enumerated_type.

One thing I pointed out about factors in their limitation is that they don't guarantee a length. It can be as long as you wish. It's not guaranteed to be length 1 of some type. Additionally for levels, we have character values only. Wouldn't it be nice if for the levels we could have some arbirtrary types?!