0

I want to implement a trait for shared behaviour among a set of models.

In particular, I have to be sure that every model implementing my trait have to be a function that returns a polars dataframe with (at least) some fixed columns.

For sake of clarity, let us suppose we have a (0,1) float column and a trait for clustering. For values less than 0.5 we will label it as "low" else "hight". An algorithm that works only on the label needs to be sure to find that column and in the trait I want to be sure that column will be there.

Is it possible to specify the list of (mandatory) columns I expect for structs implementing this trait?

Sigi
  • 53
  • 8
  • I don't think so. Maybe change your API to require a `Series` or `ChunkedArray` instead? – PitaJ Aug 10 '22 at 15:27
  • Yes, I can change my API to get a list of series but actually I need these series to be coherent (for example same len), in other words I need a df. – Sigi Aug 10 '22 at 16:13
  • I don't think there's a way to make this assertion at compile time, but you could create a wrapper struct that performs the validation you need on creation, and require the API return that struct. – PitaJ Aug 10 '22 at 16:25
  • I agree, I would document these requirements and add `assert!()` statements at the beginning of the function to check them at runtime as much as possible. I mean, latest when you try to access them, there will be a `Result` or `Option` somewhere you can check for validity. – Finomnis Aug 10 '22 at 22:24
  • Ok, so the only one possible way is to check at runtime. Thanks! – Sigi Aug 11 '22 at 20:32

0 Answers0