All examples
Language

A cyclic graph

Explore object identity and cyclic references in Aner’s managed object heap.

Download .aner source
oop_graph.aner
// Directed graph with linked adjacency lists; no array/list library is needed.
// Two connections create a cycle between vertices. neighbor_count counts only
// outgoing neighbors; it is not a graph traversal or reachability algorithm.
class Vertex {
    public let id: Int64
    public var edges: Edge?

    fn connect(target: Vertex) {
        edges = Edge(target: target, next: edges)
    }

    fn neighbor_count() {
        var count = 0
        var cursor = edges
        while cursor != null {
            let edge = cursor.unwrap()
            count = count + 1
            cursor = edge.next
        }
        return count
    }
}

class Edge {
    public let target: Vertex
    public var next: Edge?
}

let first = Vertex(id: 10, edges: null)
let second = Vertex(id: 20, edges: null)
first.connect(second)
second.connect(first)
print("First outgoing neighbors")
print(first.neighbor_count())
print("Second outgoing neighbors")
print(second.neighbor_count())
print("First edge reaches second vertex")
print(first.edges.unwrap().target == second)
print("Return edge reaches first vertex")
print(second.edges.unwrap().target == first)

Make it your experiment.

With Aner installed, save this program as examples/oop_graph.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/oop_graph.aner
aner run examples/oop_graph.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner