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 tuple return type #255

Open
katat opened this issue Jan 4, 2025 · 5 comments · May be fixed by #261
Open

Support tuple return type #255

katat opened this issue Jan 4, 2025 · 5 comments · May be fixed by #261
Labels

Comments

@katat
Copy link
Collaborator

katat commented Jan 4, 2025

Tuple return type can be useful when it needs to return more than one type from a function, avoiding the need to duplicate functions into two versions just for different return types.

For example:

fn divmod_limbs(const BITLEN: Field, dividend: [Field; DVDLEN], divisor: [Field; DVSLEN]) 
-> ([Field; (DVDLEN - DVSLEN) + 1], [Field; DVSLEN]) {
    let quotient_limbs = unsafe calculate_quotient_limbs(BITLEN, dividend, divisor);
    let rem_limbs = unsafe calculate_rem_limbs(BITLEN, dividend, divisor);
    
    ...

    return (quotient_limbs, rem_limbs);
}

Without the support of tuple type, this function has to be duplicated with the only difference in the return type.

@katat katat added the medium label Jan 4, 2025
@0xnullifier
Copy link
Contributor

0xnullifier commented Jan 8, 2025

hey @katat is it fine if I take this up?

@mimoo
Copy link
Contributor

mimoo commented Jan 8, 2025

yes it would be great to have a tuple type! sg @0xnullifier

@0xnullifier
Copy link
Contributor

0xnullifier commented Jan 9, 2025

So just some general doubts @katat @mimoo :

I have to make TyKind:Tuple(Box<TyKind>,size) The implementation will be more or less similar to TyKind::Array but without the type checking that all elements are same because the elements of tuple can be different.

The member access should be we go with the rust style like :
tuple.i -> returns the ith element of tuple
or just keep it same as the array access?

And do I have to implement destructuring also? meaning something like

fn returns_tuple() -> (Field, Bool) {}

fn main() {
    let (x , y ) = returns_tuple();
}

@mimoo
Copy link
Contributor

mimoo commented Jan 9, 2025

there should be some type checking right, as you will need to make sure that the returned tuple matches the signature, and on the caller side you need to give the correct type to the returned var.

member/array access... personally I think Rust should have done array access so I'd prefer array access :D

regarding destructuring, I'd say do what is easier for now (destructuring or access) and we can implement what's left in another PR (because both are good)

@0xnullifier
Copy link
Contributor

there should be some type checking right, as you will need to make sure that the returned tuple matches the signature, and on the caller side you need to give the correct type to the returned var.

Yup what i was trying to say for the internal structure withing in the tupple like obvs tuples of (Field,Bool) should be allowed other wise it's just an array with round brackets for declaration right?

member/array access... personally I think Rust should have done array access so I'd prefer array access :D

Got it we will do array access

regarding destructuring, I'd say do what is easier for now (destructuring or access) and we can implement what's left in another PR (because both are good

maybe if it is easier to implement then I will also try this

@0xnullifier 0xnullifier linked a pull request Jan 11, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants