Skip to content

Basics: Data Types and Ownership

Bruce Long edited this page Jan 23, 2020 · 2 revisions

When declaring variables in CodeDog, both an ownership type and data type are specified.

Data Types

The basic data types built into CodeDog, each followed by an example that declares & assigns a variable of that type.

Booleans

A boolean with possible values of "true" and "false"

me bool: isOn <- true

Text

A "char", limited to a string literal of length one. A "string" of text characters of arbitrary length.

me char: firstInitial <- "M"
me string: firstName <- "Matt"

Numeric values

C++ style numeric data types are built in, supporting signed and unsigned integers of 32 and 64 bit, as well as a double.

me int: age <- 27
me int32: 
me int64: 
me uint32: 
me uint64: 
me double: pi <- 3.1415926535897

Ranges

A range of integers can be specified. Currently this defaults to int. In the future this will be adjusted to choose an optimal data type and restrict values to the specified range.

me 0..23: hour <- 17

Ownership

In CodeDog, variables are assigned an ownership type. The ownership type of an object tells CodeDog where to allocate memory and when to release the memory. For strictly Python users, this may be a foreign concept. For C++ users, they translate to objects, pointers, shared pointers, etc. This is a part of CodeDog because it translates your program to languages that have very different approaches to ownership, without you needing to know the details of each language.

An analogy for considering ownership types for the uninitiated is to think of a person as a class, we'll call her Susie. Susie has integral components, such as an arm or head, and these are represented in CodeDog ownership as type "me". It doesn't make sense to reference them as separate from Susie, they're an integral part of her. Those are "me"s.

Susie's class may reference a belonging, such as a bike, that belongs to her but isn't an integral part of her (unless she commutes on it, in which case it may feel like an appendage!) This is represented in CodeDog ownership by "my", because it belongs to her but it isn't an integral part. It could be lent to someone else to make use of it but it isn't theirs to destroy, that right remains with Susie. She is the one that releases it from memory when it reaches the end of its life.

Susie's class may reference something that she shares with many different people, possibly at the same time. Like a bus, Susie uses the bus, but so do others. In CodeDog this ownership is "our". None of the users of "our" items have to destroy the "our" objects. The system will destroy them when the time is right.

Finally Susie's class may also contain a reference to someone else's stuff, like Dad's car. Susie can use it but it's not hers to destroy. This is represented in CodeDog ownership as "their". It points to a reference in memory allocated somewhere else that Susie isn't responsible for releasing.

In CodeDog memory releasing is done for you, so you won't be manually releasing memory. But the analogy can be useful for wrapping your head around when memory is being freed.

Common ownership types

me - a part of its class or function, this is the object itself as a local or global variable or an argument in a function. As soon as the object it is in is erased from memory or the program exits the scope it's declared in, any "me" elements are released from memory. If no ownership type is specified, CodeDog will default to "me".

our - the "our" element is a reference to a variable that other classes or functions may also have a reference to. When the program exits the scope of an "our" element, the "our" element on the stack is freed, but if other things are still referencing the same variable, the variable itself will not be freed. Only when all pointers to a location are freed will the referenced variable be freed. This is how most variables in Java work and in C++ codeDog implements and manages them as a shared pointer.

their - the "their" element is a small piece of memory that points to a location in memory on the heap that is managed by another class. In CodeDog "their" is less commonly used than "me" and "our", it is used primarily for pointers received from an API or another class. Analogous to an unmanaged pointer in C++.

Advanced ownership types

The ownership types of "my", "we" and "itr" are intended for more advanced use cases and will be explained later.

Clone this wiki locally