# Struct with optional self type member crashes

**URL:** https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336
**Category:** Mojo
**Created:** [April 29, 2025, 3:11pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336 "2025-04-29T15:11:49Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ratulb](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@ratulb](https://forum.modular.com/u/ratulb)
#### Post date: [April 29, 2025, 3:11pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/1 "2025-04-29T15:11:49Z")

</div>

I have the following mojo code - it just crashes!

```mojo

fn main():
    _e = Employee("me", None)

@value
struct Employee:
    var name: String
    var assistant: Optional[Employee]

```

How can I achieve what I am looking for?

**mojo version: mojo 25.3.0.dev2025042905 (b3907eb3)**

---

<div class="post-metadata">

### Author: ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)
#### Post date: [April 29, 2025, 3:51pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/2 "2025-04-29T15:51:24Z")

</div>

Please file a bug. This should be a compiler error instead of a crash.

The reason you can’t do this is because the type has infinite size. Unlike in Python, `Optional[Employee]` is a single allocation, which contains an `Optional[Employee]`, and so on.

This would work for you:

```auto
from memory import OwnedPointer

fn main():
    _e = Employee("me", None)

@value
struct Employee:
    var name: String
    var assistant: Optional[OwnedPointer[Employee]]

```

But right now we’re reworking parts of the standard library so there’s an interface conflict.

---

<div class="post-metadata">

### Author: ![sora](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sora](https://forum.modular.com/u/sora)
#### Post date: [April 29, 2025, 4:41pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/3 "2025-04-29T16:41:18Z")

</div>

Have you tried this code? `OwnedPointer` doesn’t type check, and `ArcPointer` seg faults. 🙃

---

<div class="post-metadata">

### Author: ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)
#### Post date: [April 29, 2025, 4:42pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/4 "2025-04-29T16:42:43Z")

</div>

See my last sentence.

---

<div class="post-metadata">

### Author: ![ratulb](https://avatars.discourse-cdn.com/v4/letter/r/13edae/32.png) [@ratulb](https://forum.modular.com/u/ratulb)
#### Post date: [April 29, 2025, 4:52pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/5 "2025-04-29T16:52:00Z")

</div>

That did not work. Below are the errors that I get:

```mojo
error: cannot bind type 'OwnedPointer[Employee]' to trait 'Copyable & Movable'
var assistant: Optional[OwnedPointer[Employee]]
                            ~~~~~~~~~~~~^~~~~~~~~~
note: struct 'OwnedPointer[T]' does not implement all requirements for 'Copyable'
note: required function ' __copyinit__' is not implemented

```

---

<div class="post-metadata">

### Author: ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)
#### Post date: [April 29, 2025, 4:53pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/6 "2025-04-29T16:53:31Z")

</div>

> But right now we’re reworking parts of the standard library so there’s an interface conflict.

What I was trying to say with this is that there isn’t a good way to do this at the moment in safe Mojo. We’re in the middle of applying some recent advancements to the type system.

---

<div class="post-metadata">

### Author: ![miktavarez](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/miktavarez/32/446_2.png) [@miktavarez](https://forum.modular.com/u/miktavarez)
#### Post date: [April 29, 2025, 5:12pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/7 "2025-04-29T17:12:33Z")

</div>

It’s not great at the moment. I opted to use a list of arcpointers for my project: [prism/src/prism/command.mojo at main · thatstoasty/prism · GitHub](https://github.com/thatstoasty/prism/blob/main/src/prism/command.mojo#L116)

Previously it was an `ArcPointer[Optional[Self]]` which worked, but `Optional[ArcPointer[Self]]` did not 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)
#### Post date: [October 26, 2025, 5:13pm UTC](https://forum.modular.com/t/struct-with-optional-self-type-member-crashes/1336/8 "2025-10-26T17:13:13Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
