> For the complete documentation index, see [llms.txt](https://furesoft.gitbook.io/socordia/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://furesoft.gitbook.io/socordia/extended/operator-overloading.md).

# Operator Overloading

Operator Overloading gives you the ability to write prettier code. Instead of writing functions you write operators.

> 💡 Overloaded operators should not have side-effects.

## Overloadable Operators

### Unary

| Kind       | Operators | Names          |
| ---------- | --------- | -------------- |
| Arithmetic | - %       | neg percentage |
| Bitwise    | \~        | bitwise\_not   |
| Logical    | !         | logical\_not   |
| Memory     | \* &      | deref addrof   |
| Other      | ?         | unpacking      |
| Other      | default   | default        |

### Binary

| Kind       | Operators  | Names                                  |
| ---------- | ---------- | -------------------------------------- |
| Arithmetic | + - \* / % | add sub mul div mod                    |
| Bitwise    | ^ & \|     | exclusive\_or bitwise\_and bitwise\_or |
| Logical    | == !=      | equality inequality                    |

## Operator call syntax

If a type has special member functions they will be treated as operators:

\| function | operator | sample | | Add | + | myList + "appendix" | | Remove | - | myList - "appendix" |

You can also overload the `implicit` operator to make your type implicit castable. The argument is the value to cast from and the return value is the result.

Overloaded operators are functions with the `operator` modifier and the name has to be one of the list above.

Here is a simple example of a `Point` struct.

```sc
public struct Point {
    let X : i32;
    let Y : i32;
}

implement Point {
    operator func add(lhs : Point, rhs : Point) -> Point {
        return Point::new(lhs.X + rhs.X, lhs.Y + rhs.Y);
    }

    operator func neg(p : Point) -> Point {
        return Point::new(-p.X, -p.Y);
    }
}
```

```sc
func main() {
    let p = Point::new(42, 5);
    let negP = -(p + p);

    print(p);
    print(negP);
}
```

## Exercices

1. Write a structure called `Vector2` and implement all neccessary operator that can be applied to a Vector
2. Which operators cannot be overloaded?
3. Why you don't want to overload the memory operators on a `Vector2` struct?
4. What should operators definitly not do and why?


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://furesoft.gitbook.io/socordia/extended/operator-overloading.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
