Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generic struct #212

Open
katat opened this issue Oct 24, 2024 · 1 comment
Open

Support generic struct #212

katat opened this issue Oct 24, 2024 · 1 comment
Labels

Comments

@katat
Copy link
Collaborator

katat commented Oct 24, 2024

Generic struct only can simplify repeated code, but also it is necessary when we want to use struct field to instantiate a generic method.

For example:

struct Uint8 {
    inner: Field,
    bit_len: Field,
}

fn Uint8.new(val: Field) -> Uint8 {
    let bit_len = 8;

    // range check
    let ignore_ = bits::to_bits(bit_len, val);

    return Uint8 {
        inner: val,
        bit_len: bit_len
    };
}

fn Uint8.less_than(self, rhs: Uint8) -> Bool {
    return comparator::less_than(self.bit_len, self.inner, rhs.inner);
}

With support of generic struct, struct Uint8 can be generalized as Uint<bit_len> generic over the size of bit len.

Also the self.bit_len can be treated as a immutable constant so it can be as a constant argument to instantiate a generic function.

@katat katat added the hard label Oct 24, 2024
@katat katat mentioned this issue Oct 24, 2024
2 tasks
@mimoo
Copy link
Contributor

mimoo commented Oct 25, 2024

Btw constants as field might still be useful. For example, if we want to track the bit number of something and only perform mod reductions or range checks when the bits get too large

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants