E1009

E1009#

The struct is never constructed.

Erroneous example#

pub struct A { // Warning: The struct A is never constructed
  value : Int
}
priv struct A { // Warning: The struct A is never constructed
  value : Int
}

fn f(a : A) -> Int {
  a.value
}

Suggestion#

The pub visibility modifier only allows external packages to access the struct, by dot-syntax (a.value), or by pattern matching. Therefore, if you expect external 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 external 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 intend 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 A {
  value : Int
}

fn A::new(value : Int) -> A {
  A::{ value }
}

fn A::value(self : A) -> Int {
  self.value
}