# E4001

A field with incompatible visibility cannot be declared within a struct.

The following visibility combinations are allowed:

- A public field within a public struct, although it will emit a warning
  [E0008](./E0008.md).
- A private field within a public struct.
- A private field within a private struct, emitting [E0008](./E0008.md) as well.

## Erroneous Example

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

This example declares a field with public visibility within
a struct with private visibility, which is not allowed and will give
the following error on line 2:

```
A public field cannot be declared within a private struct.
```

## Suggestion

Change the visibility of the field to match the visibility of the struct:

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

We could have written `priv field: Int` instead of `field: Int` as well,
but it is not necessary, as all fields in a private struct are private
by default.
