E0009

E0009#

Warning name: struct_never_constructed

The struct is never constructed.

Erroneous example#

///|
pub struct A { // Warning: the struct A is never constructed
  value : Int
}

///|
priv struct B { // Warning: the struct B is never constructed
  value : Int
}

///|
fn B::get(a : B) -> Int {
  a.value
}

Suggestion#

The pub visibility modifier only allows other packages to access the struct, by dot-syntax (a.value), or by pattern matching. Therefore, if you expect other packages to be able to construct the struct, you should use the pub(all) visibility modifier.

///|
pub(all) struct A {
  value : Int
}

However, if you do not want other packages to construct the struct, this might be due to incompleteness or bugs in your code. There are some common cases:

  • You planned to use smart constructors or factory functions to construct the struct, but you forgot or have not implemented them.

  • You intended to keep the struct private to control its instantiation but may have forgotten to provide a way to do so within its own package.

In these cases, you should consider providing a way to construct the struct within its own package.

///|
priv struct B {
  value : Int
}

///|
fn B::new(value : Int) -> B {
  B::{ value, }
}