E4053

E4053#

Compiler diagnostic name: invalid_self_type.

Invalid type for "self": must be a type constructor.

This error happens when you define a method or implement a trait for a type that is not a type constructor.

Types that are type constructors:

  • Tuple: (Int, Bool)

  • enums, structs, traits, new types (type), and error types (type!).

Types that are not type constructors:

  • Function: (Int) -> Bool

  • Type parameter: T in fn f[T](x : T) -> T

Erroneous example#

pub(open) trait T {
  f(Self) -> Unit
}

impl T for (Int) -> Unit with f(self) {
  //                         ^
  // Error: Invalid type for "self": must be a type constructor.
  self(0)
}

Suggestion#

Use a type constructor such as a struct, enum, trait, or new type. If you need to attach methods to a function value, wrap the function in a named type first.

pub(open) trait T {
  f(Self) -> Unit
}

pub struct Handler {
  run : (Int) -> Unit
}

impl T for Handler with f(self) {
  (self.run)(0)
}

fn init {
  Handler::{ run: x => ignore(x) }.f()
}