# Leno: plain classes, `@property` and `ClassVar` (D188)

Luke's D183 (3) puts Python's plain classes in Leno 1.0, after a short design note on
aliasing. This is that note, and the design implemented.

## 1. The question

A plain class is Python's ordinary class: no `@dataclass`, its fields made by `__init__`.

```python
class Counter:
    def __init__(self, start: int) -> None:
        self.n = start

    def bump(self) -> None:
        self.n += 1
```

A Python object is a reference: `a = Counter(1); b = a; b.bump()` changes the one object
both names hold. Byron's records are values. So the question for plain classes is the one
leno-sharing.md asked of dataclasses (LN12): where may two names share an object, and what
does Leno do there.

## 2. A plain class is no different from a dataclass

A dataclass is as much a reference in Python as a plain class is; `@dataclass` only writes
`__init__`, `__repr__` and `__eq__`. Everything the sharing design D (D125, D127) decides for
dataclasses holds for plain classes unchanged:

- A local bound to a path (`b = a`, `n = graph.first`) and then changed is a view of it (an
  object renaming), so the change reaches the original, as Python's shared object does.
- A local bound to an element (`n = nodes[i]`) and then changed is the element taken out and
  put back (D127).
- A store (`xs.append(a)`, `self.f = a`, a field of a constructor's call) copies a value and
  moves a limited or owned one; a later change of the original, which Python would share, is
  refused (B0401) with the `copy(...)` to write if a copy is meant.
- What a view cannot follow is refused: the name bound again, its source replaced (E10),
  `is` between objects, and shared mutable graphs (E8), which in Byron are a table and ids,
  or a proof enclave's `Ref[T]`.

So the design that keeps proof is design D for plain classes too, and needs nothing new from
the prover or the code generator: a plain class is a record type, `__init__` is the function
that makes its values, and its methods are the record's primitive operations, as a
dataclass's are.

The one case peculiar to plain classes is E7 (capture in a constructor): `__init__` may store
an argument (`self.items = items`). That is a store, under the rule above: a list argument
moves into the object (the caller's later use of it is refused, as for a dataclass's field),
a record argument is copied.

## 3. The design

- **Fields.** A plain class's fields are the names `__init__` assigns through `self` at its top
  level (`self.n = start`, `self.n: int = start`), in that order. Each field's type is its
  annotation, or the type of the parameter or value assigned where Leno's annotations fix it
  (Byron.Leno.Types); otherwise B0401 asks for `self.f: T = ...`. A field assigned only by
  another method, or inside a branch of `__init__`, is refused: every object has every field
  from the start, as a record's components are.
- **Construction.** `C(args)` calls `__init__`'s function: a new record, `__init__`'s body run
  on it, the record returned. Its parameters are `__init__`'s, with their defaults.
- **Methods**, `__str__` and the other special methods: as a dataclass's. A plain class's
  `==` is Python's identity unless it defines `__eq__`; Leno has no identity, so `==` of two
  plain objects without `__eq__` is refused (B0401), asking for `__eq__`.
- **`@property`**: a method without parameters, read as a field (`b.double`), Byron's function
  of the type (Sema's field form already resolves a name that is no component to the type's
  function). A setter (`@x.setter`) is refused: assign a field or call a method.
- **`ClassVar[T]`**: one variable of the module, shared by every object, as Python's class
  attribute is: `Box.count` and, in the class's methods, `self.count` (no field has that name)
  name it; a function that assigns it changes the module's state (its `Global`, inferred).
- **Inheritance** among plain classes is a tagged hierarchy, as among dataclasses; a subclass's
  `__init__` calls `super().__init__(...)` first. (Not in this first slice: B0401 asks for the
  hierarchy as dataclasses, which Leno takes.)

No decision is needed from Luke: the design is D, already ratified (D184), applied to a class
form whose only difference from a dataclass is who writes `__init__`.

## 4. Nested containers (LENO-15)

A list of lists (`grid: list[list[int]]`), a dict of lists and the like are Python's
everyday structures. In Python the outer list holds references to the inner lists, so
`row = grid[0]; row.append(5)` changes `grid[0]`, and `grid.append(row)` makes the two
places share one list.

Byron's `list[T]` is `Byron.Containers.Unbounded.Vectors (T).Vector`, which is limited: a
vector owns its heap storage and is copied only explicitly (`Copy`). Its `Element_Type` is a
definite, non-limited formal, so a vector of vectors is not an instance it takes. There are
two ways to give Leno Python's meaning here.

**A. A copyable list (D130).** Make the element list a copyable reclaimed type
(`Finalizable => (Adjust => ...)`): a list value can then be an element, copied where it is
stored. Python's sharing would come from design D, as for any other value: a name bound to
`grid[0]` and then changed is an element view (D127), taken out and put back. But D130's
copyable types hold no access values (their storage is the run time's, as a text's is), and a
list's elements are of any type, themselves copyable or owning: the run time would have to
copy and free elements whose operations are Ada's. That is a run-time storage model for
generic elements, which Byron does not have; it is the larger change, and it would also give
`list[T]` two representations, a user-visible change of which type `list[int]` is in Noel and
Leon. Not chosen.

**B. A container of containers (chosen).** A new generic,
`Byron.Containers.Unbounded.Nested_Vectors`, with a formal package of the inner vectors,
`with package Inner is new Vectors (<>)`. It stores the inner vectors themselves (limited
components of its storage array), and gives Python's operations by position: the outer and
inner lengths, `Element (Container, I, J)`, `Replace_Element (Container, I, J, X)`,
`Append (Container, I, X)` (`grid[i].append(x)`), `Append` of a copy of a vector
(`grid.append(row)`: Python would share it; Leno stores a copy, and a later change of `row` is
refused by design D's store rule, as for any stored value), and `Take`/`Put_Back`, which move
an inner vector out and back (a view of `grid[i]`, D127, without a copy). Its model is the
lengths and the elements by position, so a client proof reads `grid[i][j]` as Python's. Leno
maps `list[list[T]]` to an instance of it whose `Inner` is the instance `list[T]` already is,
so `row: list[int]` and `grid[i]` are of the same type, and nothing users see in Noel or Leon
changes (the generic is new). A dict of lists and a list of dicts take the same shape with the
dictionaries' package. No Core form is needed: the storage is an access to an array, as the
vectors' own, and the body is proved as theirs is.

Implemented (D188, LENO-15): the generic as described, its body proved; Leno's forms are listed
in the Leno reference, [lists of lists](../manuals/language/leno/collections.md#lists-of-lists).
