# E4061

Cannot implement a trait for a type when both are defined in other packages.

MoonBit follows the orphan rule, which means that you can only:

| Traits  | Types   | Allowed? |
|---------|---------|----------|
| Current Package | Current Package | Yes      |
| Other Package | Current Package | Yes      |
| Current Package | Other Package | Yes      |
| Other Package | Other Package | No       |

See the [Access control of methods and trait implementations](../packages.md#trait-implementations)
section of the MoonBit documentation for more information.

## Erroneous example

Suppose you have type `A` and trait `B` defined in package `a` in module
`username/hello`:

`a/top.mbt`:

```{literalinclude} /sources/error_codes/4061_error/a/top.mbt
:language: moonbit
```

`a/moon.pkg`:

```{literalinclude} /sources/error_codes/4061_error/a/moon.pkg
:language: moonbit
```

Now, if you want to implement trait `@a.B` for type `@a.A`:

`b/moon.pkg`:

```{literalinclude} /sources/error_codes/4061_error/moon.pkg
:language: moonbit
```

`b/top.mbt`:

```{literalinclude} /sources/error_codes/4061_error/top.mbt
:language: moonbit
```

MoonBit will report an error.

## Suggestion

You can move either the trait or the type into the package that contains the
implementation. If that is not possible, create a wrapper type around the type
you wish to implement the trait for, and then implement the trait for the
wrapper type:

`b/top.mbt`:

```{literalinclude} /sources/error_codes/4061_fixed/top.mbt
:language: moonbit
```
