Skip to content
New issue

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

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

Already on GitHub? # to your account

Questions around modules: type inside the module named same as the module. #80

Open
Swoorup opened this issue Feb 26, 2024 · 1 comment

Comments

@Swoorup
Copy link

Swoorup commented Feb 26, 2024

This is more of a design question.

If I have a type inside a module which is named the same as the module itself, I'd want to be able to import it twice.

  • One for importing the actual type directly;
  • Other for importing the module but fully qualifying when using any of the functions inside the module.

The following currently doesn't work and I get the error. Only approach I have found is to name the struct differently or use pascal casing.

error: required import 'fp64::fp64' not found
  ┌─ :8:20
  │
8 │             return fp64::add(a, b);
  │                    ^
  │
  = missing import 'fp64::fp64'
composer
    .add_composable_module(ComposableModuleDescriptor {
        source: r#"
      struct fp64 {
        high: f32,
        low: f32
      }

      fn add(a: fp64, b: fp64) -> fp64 {
        let high= a.high + b.high;
        let low= a.low + b.low;
        return fp64(high, low);
      }
    "#,
        file_path: "fp64.wgsl",
        as_name: Some("fp64".to_owned()),
        ..Default::default()
    })
    .unwrap();

let module = composer
    .make_naga_module(NagaModuleDescriptor {
        source: r#"
      #import fp64;
      #import fp64::fp64;

      fn test() -> fp64 {
        let a = fp64(0.0, 0.0);
        let b = fp64(0.0, 0.0);
        return fp64::add(a, b);
      }

      @group(0) @binding(1) var<uniform> bounds: fp64;
    "#,
        file_path: "",
        ..Default::default()
    })
    .map_err(|err| println!("{}", err.emit_to_string(&composer)))
    .unwrap();

What would be best approach to organize otherwise?

@robtfm
Copy link
Collaborator

robtfm commented Feb 26, 2024

it's meant to warn about duplicated import names, are you running in debug? i guess this corner case may well have been missed.

to answer the question, you could use the type qualified as fp64::fp64 instead of importing it, or you could alias either the module (#import fp64 as fp64_module) or the type (#import fp64::fp64 as fp64_t) to avoid the ambiguity when you want to use both in the same scope.

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

No branches or pull requests

2 participants