Skip to content

Const Generics #7

@jesskfullwood

Description

@jesskfullwood

I'm still interested in the ideas behind this project. Had a play to see how const generics might make the API more expressive, and I think it has a lot of potential. The idea is that const-generics would allows you to insert and retrieve columns into the dataframe by name, and it would be a compile error to ask for a column that didn't exist. Take this toy code (using frunk which is great):

#![feature(const_generics)]

use frunk::hlist::{HList, HCons, HNil, Selector};

struct Column<T, const S: &'static str> {    // S is the 'name' of the column
    inner: Vec<T>
}

impl<T, const S: &'static str> Column<T, S> {
    fn new(inner: Vec<T>) -> Self {
        Self {
            inner
        }
    }
}

struct Frame<H: HList> {
    inner: H
}

impl Frame<HNil> {
    fn new() -> Self {
        Self {
            inner: HNil
        }
    }
}

impl<H: HList> Frame<H> {
    fn add<T, const S: &'static str>(self, col: Vec<T>) -> Frame<HCons<Column<T, S>, H>> {
        let column = Column::new(col);
        Frame {
            inner: HCons{
                head: column,
                tail: self.inner
            }
        }
    }

    pub fn get<T, Index, const S: &'static str>(&self) -> &Column<T, S>
    where
        H: Selector<Column<T, S>, Index>,
    {
        Selector::get(&self.inner)
    }
}

fn main() {
    let frame = Frame::new()
        .add::<u32, "col1">(vec![1,2,3])
        .add::<u32, "col2">(vec![1,2,3]);
    let col = frame.get::<_, _, "col1">();  // OK
    // let col = frame.get::<_, _, "missing">();  // compile error!
}

This is pretty neat! Problem: it crashes with an ICE at the moment. But it will work soon!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions