lambdaclass / cairo-vm_in_go

cairo-vm_in_go is a Go implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.
Apache License 2.0
57 stars 13 forks source link

Add generic function to fetch scope variables with a generic type #293

Closed fmoletta closed 1 year ago

fmoletta commented 1 year ago

The pattern scopes.Get -> handle error -> cast -> handle error is very frequent and quite annoying. This PR adds a generic function FetchScopeVar which has the same functionality as scopes.Get, but also casts the scope variable to the generic type, handling the error. This turns code that looks like this:

nAny, err := scopes.Get("N")
    if err != nil {
        return err
    }
n, ok := nAny.(*big.Int)
    if !ok {
        return errors.New("N not in scope")
    }

To something like this:

n, err := FetchScopeVar[*big.Int]("N", scopes)
    if err != nil {
        return err
    }