Data Type Terminology

How would a rust programmer write the following in rust?

The notion of a constituent of a declared type DT is defined recursively as follows:

— DT is a constituent of DT.
— If DT is a row type, then the declared type of each field of DT is a constituent of DT.
— If DT is a collection type, then the element type of DT is a constituent of DT.
— Every constituent of a constituent of DT is a constituent of DT.

How would one represent the term "constituent" in rust syntax?

This sounds like you want to express the idea of a type T transitively containing a value of type U (where T might or might not be U). So, i32 constituent Vec<i32> or struct S { a: u8, b: f32 } f32 constituent S.

Insofar as I am aware, there is nothing like this concept in the language.

Now, that said, I think you could maybe manually construct something like this using a trait.

trait Constituent<T> {}

impl<T> Constituent<T> for T {}
impl<T> Constituent<T> for Vec<T> {}
impl Constituent<u8> for S {}
impl Constituent<f32> for S {}

That said, I get nervous every time I see both blanket and specific impls at the same time. I'm not sure the compiler will let you do that in general.

Thank you kindly Daniel. I think you are on the correct path.

It's to do with Set Theory and group theory. Wikipedia does help, but doesn't tell you much about Rust Syntax.

Background: Im building a SQL database, and I need to accurately define the Data Types. I see you used traits -- that's clever. Im familiar with Structs and Enums. Im currently still building out the structure of the SQL implementation. Ive grasped the ideas of structs and Enums -- now, I need to study traits more in depth. Ive seen some open source implementations of SQL online, which has made things easier to grasp. I prefer though to get answers through trial and error, and asking questions, rather than copying code, or copying a developers system of thought.

Thank you for taking the time to answer my question. I shall work from your answer. Thanks.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.