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

Add TypeArg::as_type_row helper for working with row variables. #1837

Open
doug-q opened this issue Jan 6, 2025 · 0 comments
Open

Add TypeArg::as_type_row helper for working with row variables. #1837

doug-q opened this issue Jan 6, 2025 · 0 comments

Comments

@doug-q
Copy link
Collaborator

doug-q commented Jan 6, 2025

I needed them out of tree and wrote:

/// Fallibly convert a [TypeArg] to a [TypeRowRV].
///
/// This will fail if `arg` is of non-sequence kind (e.g. Type).
/// TODO move this to `impl TypeArg` in `hugr-core`
pub fn type_arg_into_type_row_rv(arg: impl Into<TypeArg>) -> Option<TypeRowRV> {
    match arg.into() {
        TypeArg::Sequence { elems } => elems
            .into_iter()
            .map(type_arg_into_type_rv)
            .collect::<Option<Vec<_>>>()
            .map(|x| x.into()),
        TypeArg::Variable { v } => {
            Some(vec![TypeRV::new_row_var_use(v.index(), v.bound_if_row_var()?)].into())
        }
        _ => None,
    }
}

/// Fallibly convert a [TypeArg] to a [TypeRow].
///
/// This will fail if `arg` is of non-sequence kind (e.g. Type)
/// or if the sequence contains row variables.
/// TODO move this to `impl TypeArg` in `hugr-core`
pub fn type_arg_into_type_row(arg: impl Into<TypeArg>) -> Option<TypeRow> {
    match arg.into() {
        TypeArg::Sequence { elems } => elems
            .into_iter()
            .map(type_arg_into_type)
            .collect::<Option<Vec<_>>>()
            .map(|x| x.into()),
        _ => None,
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant