# XAML schema

The package `moonrockz/xaml/schema`.

The registered types, members, directives and namespaces that give a XAML document its meaning. The
[README of the module](../../README.mbt.md#the-model) describes them. To read and write your own structs you usually need none of
it: [a codec](../README.mbt.md) registers them. This page is about what a codec cannot do:
[types registered by hand](#types-registered-by-hand), and
[dependency objects](../../../dependency-object/src/README.mbt.md), which are the [controls](#controls) that a document creates.

In the examples `@dependency` is `moonrockz/dependency-object`, `@events` and `@path` are its packages `events` and `path`, `@xaml` is
`moonrockz/xaml`, and `@schema` and `@nodes` are its packages `schema` and `nodes`.

## Types registered by hand

A `XamlTypeDefinition` has the name of a type in XAML, and the constructors, setters and getters to call. `SchemaContextBuilder` puts
the definitions in a schema context, which `parse`, `load` and `save` take. You need them for:

- Objects that refer to each other in a circle, such as a parent that its children have.
- Filling an object that exists, and other objects that you change while a document is read.
- Constructors with arguments and factory methods.
- [Dependency objects](#controls) and [events](#events).
- Attached members, such as `Grid.Row`.
- Your own [markup extensions](../writer/README.mbt.md#markup-extensions-and-converters), such as `{Binding Path}`.
- Types with base types, generic types and name scopes of their own.

The types in one document are all registered by hand or all described by codecs.

The example does it for a `Task`, which has an id, a title, a task that comes before it, and smaller tasks:

```mbt check
///|
priv struct Task {
  mut id : String
  mut title : String
  mut done : Bool
  mut after : @nodes.XamlValue
  tasks : Array[@nodes.XamlValue]
}

///|
let task_type : @nodes.XamlTypeName = @nodes.XamlTypeName::new(
  "urn:tasks", "Task",
)

///|
/// Puts a `Task` in a XAML value and takes it out again.
let task_key : @nodes.ObjectKey[Task] = @nodes.ObjectKey::new()

///|
fn task_of(target : @nodes.XamlObject) -> Task raise {
  guard task_key.unwrap(target) is Some(task) else {
    raise @schema.ValueError("not a Task")
  }
  task
}

///|
fn task_context() -> @schema.SchemaContext raise {
  let task = @schema.XamlTypeDefinition::new("urn:tasks", "Task")
    .object_key(task_key)
    .add_constructor([], (_, _) => {
      let task = { id: "", title: "", done: false, after: Null, tasks: [], }
      Object(task_key.wrap(task_type, task))
    })
    .add_member(
      "Id",
      value_type=@schema.string_type,
      getter=target => String(task_of(target).id),
      setter=(target, value) => {
        if value is String(id) {
          task_of(target).id = id
        }
      },
      default_value=String(""),
    )
    // `{x:Reference docs}` finds the task with the id `docs`.
    .aliased_member(Name, "Id")
    .add_member(
      "Title",
      value_type=@schema.string_type,
      getter=target => String(task_of(target).title),
      setter=(target, value) => {
        if value is String(title) {
          task_of(target).title = title
        }
      },
    )
    .add_member(
      "Done",
      value_type=@schema.boolean_type,
      getter=target => Bool(task_of(target).done),
      setter=(target, value) => {
        if value is Bool(done) {
          task_of(target).done = done
        }
      },
      default_value=Bool(false),
    )
    .add_member(
      "After",
      value_type=task_type,
      getter=target => task_of(target).after,
      setter=(target, value) => task_of(target).after = value,
      default_value=Null,
    )
    // The elements in a `Task` element are its smaller tasks.
    .collection(
      (target, item) => task_of(target).tasks.push(item),
      item_type=task_type,
    )
    .item_enumerator(target => ReadOnlyArray::from_array(task_of(target).tasks))
  @schema.SchemaContextBuilder::new().add_type(task).build()
}

///|
test "parse, change and save" {
  let context = task_context()
  let text =
    #|<Task xmlns="urn:tasks" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Release">
    #|  <Task Id="docs" Title="Write the docs" />
    #|  <Task Title="Publish" After="{x:Reference docs}" />
    #|</Task>
  let { root, .. } = @xaml.parse(text, context)
  guard root is Object(release) &&
    task_of(release).tasks is [docs, Object(publish)] else {
    fail("no tasks")
  }
  assert_eq(task_of(publish).title, "Publish")
  // `After` has the same object as the list.
  assert_true(task_of(publish).after == docs)
  task_of(publish).done = true
  inspect(
    @xaml.save(root, context),
    content=(
      #|<Task xmlns="urn:tasks" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Release">
      #|  <Task Id="docs" Title="Write the docs"/>
      #|  <Task After="{x:Reference docs}" Done="True" Title="Publish"/>
      #|</Task>
    ),
  )
}
```

`Done` and `After` have default values, so `save` writes them only when they differ. `After` has the object of another
element, so `save` writes a reference to it. An object that is in several places and has no name gets one, such as
`__ReferenceID0`.

To copy objects without a text, load from an object reader:

```mbt check
///|
test "copy objects" {
  let context = task_context()
  let { root, .. } = @xaml.parse(
    "<Task xmlns='urn:tasks' Title='Release' />", context,
  )
  let settings = @reader.ObjectReaderSettings::new(construction_first=true)
  let reader = @reader.XamlObjectReader::new(root, context, settings~)
  let copy = @xaml.load(reader, context).root
  guard copy is Object(task) else { fail("no task") }
  assert_eq(task_of(task).title, "Release")
  assert_true(copy != root)
}
```

### Fill an object that exists

`parse` usually creates the root. With `root_object` in the settings it sets the members of your object, so a function that
makes an object can fill it from a document:

```mbt check
///|
/// A `Task` with the title and the smaller tasks of a release.
fn new_release(text : String) -> Task raise {
  let release = {
    id: "release",
    title: "",
    done: false,
    after: Null,
    tasks: [],
  }
  let root_object = task_key.wrap(task_type, release)
  let settings = @xaml.LoadSettings::new(root_object~)
  ignore(@xaml.parse(text, task_context(), settings~))
  release
}

///|
test "fill an object" {
  let release = new_release(
    (
      #|<Task xmlns="urn:tasks" Title="Release">
      #|  <Task Title="Write the docs" />
      #|</Task>
    ),
  )
  assert_eq(release.id, "release")
  assert_eq(release.title, "Release")
  assert_eq(release.tasks.length(), 1)
}
```

When the document has an error, `parse` raises it and the object keeps what was set before. So `new_release` gives no
object then. [The object writer](../writer/README.mbt.md#a-root-object) says which types fit, what `x:Class` does, and why text
alone, as in `<Point>1,2</Point>`, gives another object.

## Controls

The example has a panel, a card with one element in it, and a button. A `Width` is a dependency property of all three, and the
button has a `Click` for the [events](#events) below.

First come the owner types, properties and events, which have nothing to do with XAML:

```mbt check
///|
let element_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Element",
  element=true,
)

///|
let button_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Button",
  base=element_owner,
)

///|
let card_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Card",
  base=element_owner,
)

///|
let stack_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Stack",
  base=element_owner,
)

///|
let width : @dependency.DependencyProperty[Double] = try! @dependency.register(
  "Width",
  element_owner,
  @dependency.double_key,
  default_value=0,
)

///|
let click : @events.RoutedEvent[Unit] = try! @events.register_routed_event(
  "Click",
  Bubble,
  button_owner,
  @dependency.unit_key,
)

///|
let element_key : @dependency.ValueKey[@dependency.DependencyObject?] = @dependency.ValueKey::new(
  "Element",
)

///|
let content : @dependency.DependencyProperty[@dependency.DependencyObject?] = try! @dependency.register(
  "Content",
  card_owner,
  element_key,
  default_value=None,
  metadata=@dependency.PropertyMetadata::new(logical_content=value => value),
)

///|
priv struct Stack {
  object : @dependency.DependencyObject
  children : @dependency.ElementCollection
}

///|
let stack_key : @dependency.ValueKey[Stack] = @dependency.ValueKey::by_identity(
  "Stack",
)

///|
fn Stack::new() -> Stack raise {
  let object = @dependency.DependencyObject::new(stack_owner)
  let stack = { object, children: @dependency.ElementCollection::new(object), }
  object.set_host(stack_key.wrap(stack))
  stack
}

///|
/// The `Stack` of its XAML object.
fn stack_of(value : @nodes.XamlValue) -> Stack raise {
  guard @schema.as_dependency_object(value) is Some(object) &&
    object.host() is Some(host) &&
    stack_key.unwrap(host) is Some(stack) else {
    raise @schema.ValueError("not a Stack")
  }
  stack
}
```

Then each control gets a XAML type. `dependency_objects` names its owner type and makes an object of it, and
`add_dependency_member` maps a member to a property. The last argument converts the values of the property.

```mbt check
///|
fn controls() -> @schema.SchemaContext raise {
  let type_name = name => @nodes.XamlTypeName::new("urn:controls", name)
  let control = (name, owner, create) => {
    @schema.XamlTypeDefinition::new(
      "urn:controls",
      name,
      base_type=type_name("Element"),
    ).dependency_objects(owner, create~)
  }
  let element = @schema.XamlTypeDefinition::new("urn:controls", "Element")
    .dependency_objects(element_owner)
    .add_dependency_member("Width", width, @schema.double_conversion)
  let button = control("Button", button_owner, () => {
    @dependency.DependencyObject::new(button_owner)
  }).add_routed_event_member("Click", click.info())
  let card = control("Card", card_owner, () => {
      @dependency.DependencyObject::new(card_owner)
    })
    .add_dependency_member(
      "Content",
      content,
      @schema.PropertyConversion::dependency_object(type_name("Element")).optional(),
    )
    .content_property("Content")
  let stack = control("Stack", stack_owner, () => Stack::new().object)
    .add_member(
      "Children",
      value_type=@schema.element_collection_type,
      visibility=Content,
      getter=target => {
        @schema.element_collection_value(stack_of(Object(target)).children)
      },
    )
    .content_property("Children")
  @schema.SchemaContextBuilder::new()
  .add_type(element)
  .add_type(button)
  .add_type(card)
  .add_type(stack)
  .build()
}

///|
test "parse, change and save controls" {
  let context = controls()
  let text =
    #|<Stack xmlns="urn:controls" Width="200">
    #|  <Card>
    #|    <Button Width="50"/>
    #|  </Card>
    #|</Stack>
  let { root, .. } = @xaml.parse(text, context)
  let stack = stack_of(root)
  guard stack.object.logical_children() is [card] &&
    card.get_value(content) is Some(button) else {
    fail("no card with a button")
  }
  assert_eq(button.get_value(width), 50)
  button.set_value(width, 60)
  stack.children.add(@dependency.DependencyObject::new(button_owner))
  inspect(
    @xaml.save(root, context),
    content=(
      #|<Stack xmlns="urn:controls" Width="200">
      #|  <Card>
      #|    <Button Width="60"/>
      #|  </Card>
      #|  <Button/>
      #|</Stack>
    ),
  )
  // native and wasm do not free a tree by themselves.
  stack.children.clear()
  card.clear_value(content)
  stack.object.release_host()
}
```

[Memory](../../../dependency-object/src/README.mbt.md#memory) says what the last lines are for.

## Members

A `PropertyConversion[T]` has the XAML type of the values, and converts them to the type `T` of the property and back. Text in a
document first converts to the XAML type, as for every member.

| Conversion | Type of the property |
| --- | --- |
| `bool_conversion`, `byte_conversion`, `int16_conversion`, `int_conversion`, `int64_conversion`, `float_conversion`, `double_conversion`, `char_conversion`, `string_conversion` | `Bool`, `Byte`, `Int16`, `Int`, `Int64`, `Float`, `Double`, `Char` and `String` |
| `decimal_conversion`, `time_span_conversion`, `uri_conversion` | `XamlDecimal`, `XamlTimeSpan` and `XamlUri` |
| `PropertyConversion::dependency_object(type)` | `DependencyObject`, for types with `dependency_objects` |
| `PropertyConversion::object(type, key)` | the type of `key`, with which the objects of the XAML type are wrapped |
| `PropertyConversion::value(type)` | `XamlValue`, for values of several types and enumerations. `xaml_value_key` is the key for such a property |
| `PropertyConversion::new(type, from_xaml, to_xaml)` | any other |
| `conversion.optional()` | an option of the type of `conversion`. `None` is null in a document |

`add_attached_dependency_member` adds an attached member, such as `Grid.Row`. A document can set it on every object that has a
dependency object, also when the object has nothing to do with `Grid`. The type that has the member names the owner type of the
property itself, with `dependency_objects` or `dependency_owner`.

- Loading sets the property. An error of its validation or its callbacks is an error at the value in the document.
- A read-only property has no setter. When it has a collection or a dictionary, the document adds to the one that the object has,
  which is the effective value. Make it in `create`, because a default value is the same one for all objects.
- All options of `add_member` for the text of values and for saving are there too, but for a default value. See
  [Saving](#saving).

An `ElementCollection` is the children of a panel. Give it with `element_collection_value` from the getter of a member of the
type `element_collection_type`. Its items are elements of types with `dependency_objects`.

A child is in its parent when its end callback runs, and before its members are set when its type has
`usable_during_initialization`. Members of the parent that come later in the document are not set then.

## Events

A document adds handlers to the [routed events](../../../dependency-object/src/events/README.mbt.md) of its controls by their names. In
WPF the handlers are methods of the window that the document belongs to. MoonBit cannot find a method by its name, so the window
lists them in a `HandlerTable`, with a line for each:

```mbt check
///|
priv struct Counter {
  mut count : Int32
}

///|
fn Counter::handlers(self : Counter) -> @schema.HandlerTable raise {
  let handlers = @schema.HandlerTable::new()
  handlers.add("OnAdd", @dependency.unit_key, (_, _) => self.count += 1)
  handlers.add("OnReset", @dependency.unit_key, (_, args) => {
    self.count = 0
    args.set_handled(true)
  })
  handlers
}

///|
test "handlers of a document" {
  let counter = { count: 5, }
  let { root, .. } = @xaml.parse(
    (
      #|<Stack xmlns="urn:controls" Button.Click="OnAdd">
      #|  <Button Click="OnReset"/>
      #|  <Button/>
      #|</Stack>
    ),
    controls(),
    settings=@xaml.LoadSettings::new(
      event_handlers=counter.handlers().to_handle(),
    ),
  )
  let stack = stack_of(root)
  guard stack.object.logical_children() is [reset, add] else {
    fail("no buttons")
  }
  for button in [reset, add, add] {
    @events.raise_event(button, @events.RoutedEventArgs::new((), event=click))
  }
  assert_eq(counter.count, 2)
  stack.children.clear()
  stack.object.release_host()
}
```

`add_routed_event_member` gives an event to a type, as `Click` of the buttons. `Button.Click` is the same event on an element of
another type, here a panel that hears of the clicks in it. A type can have an event that another owner type registered.

- `Click="OnAdd"` and `<Button.Click>OnAdd</Button.Click>` are the same. The handler is added to the element that has the
  member, as `add_handler` does without `handled_events_too`.
- The table finds a handler by its name and by the key of its data. So one name can have a handler for each type of data, as a
  method with overloads, and one handler serves all events with its key. `add_routing` adds a handler for events with any data,
  as a method that takes `RoutedEventArgs` in WPF.
- A handler name is a name as in C#, which is the pattern of [MS-XAML-2017] 7.4.14. Upper and lower case differ.
- A name that the table does not have, a handler for other data and an object that is no element are errors at the member. So is a
  load without a table, when the document has an event.
- A value that is no handler name, such as a markup extension, is an error at the value. Nothing in it is created.
- `save` writes no handlers, as in WPF. `release_handlers` of an element removes those that a document has added, which a program on
  native and wasm needs for a handler that uses its own element.

## Property paths

`property_path_type` is a type whose values are [property paths](../../../dependency-object/src/path/README.mbt.md), as the
`PropertyPath` of WPF. Add it to your own namespace. Here a highlight has the path to what it shows:

```mbt check
///|
let highlight_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Highlight",
  base=element_owner,
)

///|
let path_key : @dependency.ValueKey[@path.PropertyPath?] = @dependency.ValueKey::new(
  "PropertyPath",
)

///|
let target : @dependency.DependencyProperty[@path.PropertyPath?] = try! @dependency.register(
  "Target",
  highlight_owner,
  path_key,
  default_value=None,
)

///|
fn highlights() -> @schema.SchemaContext raise {
  let path_type = @schema.property_path_type("urn:controls", "PropertyPath")
  let element = @schema.XamlTypeDefinition::new("urn:controls", "Element").dependency_objects(
    element_owner,
  )
  let highlight = @schema.XamlTypeDefinition::new("urn:controls", "Highlight")
    .dependency_objects(highlight_owner, create=() => {
      @dependency.DependencyObject::new(highlight_owner)
    })
    .add_dependency_member(
      "Target",
      target,
      @schema.PropertyConversion::object(
        @nodes.XamlTypeName::new("urn:controls", "PropertyPath"),
        @schema.property_path_key,
      ).optional(),
    )
  @schema.SchemaContextBuilder::new()
  .add_type(path_type)
  .add_type(element)
  .add_type(highlight)
  .build()
}

///|
test "a path in a document" {
  let context = highlights()
  let { root, .. } = @xaml.parse(
    (
      #|<c:Highlight xmlns:c="urn:controls" Target="(c:Element.Width)"/>
    ),
    context,
  )
  guard @schema.as_dependency_object(root) is Some(highlight) &&
    highlight.get_value(target) is Some(path) else {
    fail("no path")
  }
  let button = @dependency.DependencyObject::new(button_owner)
  button.set_value(width, 50)
  assert_eq(
    path.get(@dependency.object_key.wrap(button)).read(@dependency.double_key),
    Some(50),
  )
  inspect(
    @xaml.save(root, context),
    content=(
      #|<Highlight xmlns="urn:controls" Target="(Element.Width)"/>
    ),
  )
}
```

- A type in a path has the prefix that it has in the rest of the document. The type of a member, such as `c:Element`, has a
  `dependency_owner`. It does not need a XAML member for the property.
- A type of an argument, as in `Items[(x:Int32)1]`, has a conversion from text, which gets no context, and an
  [`any_value_converter`](#values-of-unknown-type).
- A wrong path is an error at the value, with the place in the path.
- `save` writes the type on which the member was registered, with the prefix of the new document. A path that a program made can have
  types without a XAML type. These are errors.
- The paths work with the registry of the process, or the one that you pass with `registry~`.

## Saving

`save` writes what `XamlWriter.Save` of WPF writes:

- A property with a local value is written, also when that equals the default value. Default values and inherited values are not.
  The local value is written, and not the one that coercion makes of it.
- A value of `set_current_value` is written too. It is a local value when the document is read again.
- A read-only property is written only when it has a collection or a dictionary with contents, as the contents of its local value.
  `visibility=Hidden` prevents it.
- An attached property is written on an object of any type, when a type that names the owner type of the property has an attached
  member for it. One that was added to a second owner type still has the name of the first one.
- An attached property that is a member of the type of the object too, such as `FontSize` of a text box, is written as this member.
- `should_serialize` of a member gets the dependency object, and prevents that the member is written when it gives `false`.

A type can also register an `attached_member_store` for attached members that are no dependency properties. Entries for those that
are dependency properties are ignored, because the dependency object has their values.

A changed callback can undo what the document has set before. Members that are attributes are written in the order of their
names, unless `depends_on` says otherwise.

## Types of dependency objects

`dependency_objects` says that the objects of a XAML type are dependency objects of an owner type, and how to make one:

```mbt check
///|
let badge_owner : @dependency.OwnerType = try! @dependency.register_owner_type(
  "Badge",
)

///|
test "an object from a document" {
  let badge = @schema.XamlTypeDefinition::new("urn:badges", "Badge").dependency_objects(
    badge_owner,
    create=() => @dependency.DependencyObject::new(badge_owner),
  )
  let context = @schema.SchemaContextBuilder::new().add_type(badge).build()
  let { root, .. } = @xaml.parse("<Badge xmlns='urn:badges'/>", context)
  guard @schema.as_dependency_object(root) is Some(object) else {
    fail("no dependency object")
  }
  assert_eq(object.owner_type(), badge_owner)
  // `xaml_object` gives the XAML value of an object, also of one that no document made.
  assert_eq(root, Object(context.xaml_object(object)))
}
```

A document has no owner types, and a dependency object knows no XAML type. So give each XAML type an owner type of its own, as
each class of WPF has one.

| Function | What it gives |
| --- | --- |
| `as_dependency_object(value)` | the dependency object in the value of such a type |
| `SchemaContext::xaml_object(object)` | the XAML object of a dependency object |
| `SchemaContext::xaml_type_for_owner(owner)` | the XAML type that names `owner` itself. Of several it is the first one that was added |
| `XamlType::dependency_owner()` | the owner type of a XAML type, its own or that of its nearest base type with one |
| `XamlType::declared_dependency_owner()` | the owner type that the XAML type names itself |
| `XamlType::dependency_object(handle)` | the dependency object of an object of any type that has one |

Two more kinds of types use `dependency_owner` instead:

- A type that only gives its name to attached properties, such as the static class `TextOptions` of WPF: `dependency_owner(owner)`.
- A type whose objects contain a dependency object and have an object key of their own: `dependency_owner(owner, object=f)`, where
  `f` finds it. Derived types inherit the function, and one with another key needs a [base view](../../README.mbt.md#the-model).
  `xaml_object` does not work for these objects.

## Values of unknown type

`XamlType::any_value_converter` converts a value of a XAML type to an `AnyValue`, for code that has no property with a type. The
types of the XAML language have these:

| Type | Key of the result |
| --- | --- |
| `x:Boolean`, `x:Byte`, `x:Int16`, `x:Int32`, `x:Int64`, `x:Single`, `x:Double`, `x:String` | `bool_key`, `byte_key`, `int16_key`, `int_key`, `int64_key`, `float_key`, `double_key` and `string_key` of `moonrockz/dependency-object` |
| `x:Char` | `char_key` there. Half of a surrogate pair is an error |
| `x:Decimal`, `x:TimeSpan`, `x:Uri` | `decimal_value_key`, `time_span_value_key`, `uri_value_key` |
| `x:Object`, for a value with a type that has a converter | that of the converter |
| `x:Object`, for another object with a dependency object | `object_key` of `moonrockz/dependency-object` |
| `x:Object`, for every other object | `xaml_object_key` |
| `x:Object`, for null | `null_value_key` |

Every other value is an error that names the type and the value. Give a type of your own a conversion with the
`any_value_converter` of its definition. Derived types inherit it.

```mbt check
///|
test "a value of unknown type" {
  let context = @schema.SchemaContextBuilder::new().build()
  guard context.get_type(@schema.int32_type) is Some(int32) &&
    int32.any_value_converter() is Some(convert) else {
    fail("no converter")
  }
  assert_eq(@dependency.int_key.unwrap(convert(Int(42))), Some(42))
}
```

## Differences from WPF

MoonBit has no reflection and no inheritance between structs. So:

1. Members and types are registered. An attached property without a registered member of its owner type is not saved, where WPF uses
   the name of its CLR type.
2. The XAML object of an element is made from its dependency object each time. All of them are equal, and names and references find
   them. An object whose XAML type does not stand for its owner type cannot be in an `ElementCollection` or in a property with
   `PropertyConversion::dependency_object`, because its type would be lost.
3. `should_serialize` can only prevent that a member is written. In WPF there are forms of `ShouldSerializeX`, and a virtual method,
   that decide alone.
4. A `Char` is a whole character, so half of a surrogate pair in an `x:Char` is an error.
5. Where WPF asks a CLR getter for a collection that is null, so that the getter can make it, `create` has to make it before.
6. Handlers are in a `HandlerTable`. In WPF they are methods of the root object, whose types have to match the event.
7. An event is registered as a member of one type or more. WPF finds every routed event by its name and owner type.

A property path is saved with a `^` before each character that needs one, and without what means nothing, such as a space around a
name. WPF writes the names and arguments as they are, and cannot always read them again.

A markup extension cannot give a handler, as it can in WPF when it returns a delegate. Documents with events need no `x:Class`, as in
System.Xaml. [MS-XAML-2017] 6.2.1.2 demands one.

Bindings and resource references do not exist yet, so nothing of them is saved. Values themselves differ from WPF in
[one case](../../../dependency-object/src/README.mbt.md#differences-from-wpf): a current value over an inherited one stays, and is then
saved.
