## `newDomain` -- create a new data type (domain)

#### Introduction

`newDomain(`k`)` creates a new domain with key `k`.

`newDomain(`k, T`)` creates a copy of the domain `T` with new key `k`.

`newDomain(`k, t`)` creates a new domain with key `k` and slots from the table `t`.

#### Call(s)

`newDomain(k)`
`newDomain(k, T)`
`newDomain(k, t)`

#### Parameters

`k` | - | an arbitrary object; typically a string
`T` | - | a domain
`t` | - | the slots of the domain: a table

#### Returns

an object of type `DOM_DOMAIN`.

#### Further Documentation

The document ``Axioms, Categories and Domains'' is a detailed technical reference for domains.

#### Related Functions

`DOM_DOMAIN`, `domain`, `domtype`, `new`, `slot`

#### Details

- Data types in MuPAD are called domains. `newDomain` is a low-level function for defining new data types. Cf. the corresponding entry in the Glossary for links to documentation about domains and more comfortable ways of defining new data types. The help page of `DOM_DOMAIN` contains a tutorial example for defining a new domain via `newDomain`.

- Technically, a domain is something like a table. The entries of this table are called slots or methods. They serve for extending the functionality of standard MuPAD functions, such as the arithmetic operations `+` and `*`, the special mathematical functions `exp` and `sin`, or the symbolic manipulation functions `simplify` and `normal`, to objects of a domain in a modular, object-oriented way, without the need to modify the source code of the standard function. This is known as overloading.

  The function `slot` and the equivalent operator `::` serve for defining and accessing a specific slot of a domain. The function `op` returns all slots of a domain.

- Each domain has a distinguished slot `"key"`, which is its unique identification. There can be no two different domains with the same key. Typically, but not necessarily, the key is a string. However, the key serves mainly for internal and output purposes. Usually a domain is assigned to an identifier immediately after its creation, and you access the domain via this identifier.

- If a domain with the given key already exists, `newDomain(`k`)` returns that domain; both other forms of calling `newDomain` yield an error.

- `newDomain` is a function of the system kernel.

#### Example 1

We create new domain with key `"my-domain"`. This key is also used for output, but without quotes:

    >> T := newDomain("my-domain")

                                     my-domain

You can create elements of this domain with the function `new`:

    >> e := new(T, 42);
       domtype(e)

                                new(my-domain, 42)

                                     my-domain

With the slot operator `::`, you can define a new slot or access an existing one:

    >> op(T)

                                "key" = "my-domain"

    >> T::key, T::myslot

                                 "my-domain", FAIL

    >> T::myslot := 42: op(T)

                        "myslot" = 42, "key" = "my-domain"

    >> T::myslot^2

                                       1764

If a domain with key `k` already exists, then `newDomain(`k`)` does not create a new domain, but returns the existing domain instead:

    >> T1 := newDomain("my-domain"):
       op(T1)

                        "myslot" = 42, "key" = "my-domain"

Note that you cannot delete a domain; the command `delete T` only deletes the value of the identifier `T`, but does not destroy the domain with the key `"my-domain"`:

    >> delete T, T1:
       T2 := newDomain("my-domain"):
       op(T2);
       delete T2:

                        "myslot" = 42, "key" = "my-domain"

#### Example 2

There cannot exist different domains with the same key at the same time. Defining a slot for a domain implicitly changes all identifiers that have this domain as their value:

    >> T := newDomain("1st"): T1 := T:
       op(T);
       op(T1);

                                   "key" = "1st"

                                   "key" = "1st"

    >> T1::mySlot := 42:
       op(T);
       op(T1);

                           "mySlot" = 42, "key" = "1st"

                           "mySlot" = 42, "key" = "1st"

To avoid this, you can create a copy of a domain. You must reserve a new, unused key for that copy:

    >> T2 := newDomain("2nd", T):
       T2::anotherSlot := infinity:
       op(T);
       op(T2);

                           "mySlot" = 42, "key" = "1st"

              "anotherSlot" = infinity, "mySlot" = 42, "key" = "2nd"

    >> delete T, T1, T2:

#### Example 3

You can provide a domain with slots already when creating it:

    >> T := newDomain("3rd",
         table("myslot" = 42, "anotherSlot" = infinity)):
       op(T);
       T::myslot, T::anotherSlot

              "key" = "3rd", "anotherSlot" = infinity, "myslot" = 42

                                   42, infinity

    >> delete T:

#### Changes

- used to be called `domain`

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000