# More magics on Option\<T\>?

**URL:** <https://internals.rust-lang.org/t/more-magics-on-option-t/7837>\
**Category:** language design\
**Created:** [June 30, 2018, 2:15am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837 "2018-06-30T02:15:59Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [June 30, 2018, 2:15am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/1 "2018-06-30T02:15:59Z")

</div>

There are postponed [RFCs](https://github.com/rust-lang/rfcs/pull/1646) for `&move`. The idea of this is to let the function receives a `&move T` reference have the right to drop the object.

If `T` is `Sized`, there is a way of doing it in today’s Rust:

```rust
fn drop_a_value<T>(obj: &mut Option<T>)
where
    T: Sized
{
    *obj = None;
}

```

This does not help when we want to use trait objects, because they are not `Sized`.

So I am thinking instead of introducing something like `&move`, we can actually make `&mut Option<dyn Trait>` a proper trait object, and allow it to be a method receiver:

```rust
impl MyTrait for Whatever {
    fn my_method(self: &mut Option<Self>,...) ...

...
}

```

Of cause, this method will need to check whether `self` is `Some` otherwise it will `panic`. But the magic here is that when we write the following:

```rust
let foo: &mut Option<dyn Foo> = &mut Some(ConcretFoo::new());

```

we first construct an object of type `ConcretFoo`, and then create a `Option<ConcretFoo>` variant `Some`. Then construct a vtable from `ConcretFoo`'s `Foo` implementation, to put in the fat pointer `foo`.

If we write

```rust
let foo: &mut Option<dyn Foo> = &mut (None as Option<ConcretFoo>)

```

It will create a `None` variant, and then use the vtable for `ConcretFoo` for the fat reference. Technically this is a “bare” vtable for a specific trait implementation, because it does not point to a real object.

The last case is

```rust
let foo: &mut Option<dyn Foo> = &mut None;

```

Intuitively, this will use the implementation of `Foo` for `!`, because there is no further constraint on the concret type can exist except that it must implement `Foo`.

All above should be the same when use assignment. So the concret type the trait object points to will vary.

For consistancy, I would like to see `&Option<dyn Trait>` can do the same thing.

---

<div class="post-metadata">

**Author:** ![sportzer](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sportzer/32/4034_2.png) [@sportzer](https://internals.rust-lang.org/u/sportzer)\
**Post date:** [June 30, 2018, 9:13pm UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/2 "2018-06-30T21:13:09Z")

</div>

What about [RFC 1909](https://github.com/rust-lang/rfcs/blob/master/text/1909-unsized-rvalues.md)? My understanding of it is that once it’s implemented it’ll effectively be &move for unsized values.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [June 30, 2018, 9:38pm UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/3 "2018-06-30T21:38:59Z")

</div>

There’s also the small detail that `Option` does not declare `T: ?Sized`. Maybe what you want is `Option<Box<dyn Foo>>`, whose contents can be destroyed with your `drop_a_value` function?

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 4, 2018, 11:55pm UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/4 "2018-07-04T23:55:28Z")

</div>

Mentioning `&move` is not the main interest of this idea. I am more interested in making `&mut Option<dyn T>` a proper trait object.

Once we have this, we can have something asked frequently by different language users, includes but not limited to C++/Java/C#, and Rust - The so called “static virtual function”. That said, we can now put “static” methods (methods without receiver) to the vtable as well as other usual vtable functions. The only thing we need is to derive a method with receiver `&mut Option<Self>`, and delegate to the static method, so it will work properly even when the user passes a `None` of a concert type.

This feature was asked and almost always receives “NO” as answer in all languages I saw, and the workaround are always something like singleton/monostate patterns.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [July 5, 2018, 12:12am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/5 "2018-07-05T00:12:52Z")

</div>

1. What would a “static virtual function” involving `&mut Option<Self>` even do? What’s an example of a use case for it? The way you just described it, it sounds like a function that acts like an ordinary method if called from a method, and does nothing if called from anywhere else. I must be missing something huge since that doesn’t make any sense.

2. Are you sure there really are users asking for this functionality? In C++/Java/C#, every time I’ve seen someone ask to do more things in static functions, they really just didn’t understand what a static function was in the first place (e.g., they were also the kind of novice that asks “why can’t static functions call non-static functions?” as if it’s not a tautology). Especially in Java/C# where the lack of free functions encourages such confusion.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 5, 2018, 1:33am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/6 "2018-07-05T01:33:57Z")

</div>

It sounds like what @earthengine wants is the ability to make static/free function calls dynamic over a trait implementation. They want a vtable that doesn’t take a `self`. [This can be done with generics](https://play.rust-lang.org/?gist=16dc3b70942270f4e092df095e93c3f1&version=stable&mode=debug&edition=2015) or [with ZST receivers](https://play.rust-lang.org/?gist=f20c568c55c80d4d2fbe1370e4a72b80&version=stable&mode=debug&edition=2015).

You could argue that the ZST receiver is wasteful here, because the size of the `&dyn Trait` is `(ptr: usize, vtable: usize)` where the actual pointer is never used, just the vtable. However, I don’t see how `&mut Option<dyn Trait>` as a receiver would change anything.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 5, 2018, 3:29am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/7 "2018-07-05T03:29:05Z")

</div>

OK. I will explain.

The idea of “static virtual function” is to do dynamic dispatch depending on the concrete type, not the value. A classic example is the “virtual constructor”: You want to create an instance of a `Pet`, but until runtime (the user selection) you don’t know whether you should create a `Cat` or a `Dog`.

In all languages I mentioned including Rust, this is done by introducing a Factory pattern.

But with my proposal, we can write

```rust
trait Pet{
    //derive a method for
    //fn new_pet(self: &mut Option<Self>);
    // and mark the original method out of the vtable
    #[derive(option_factory)]
    fn new_pet() -> Self;
}

struct Dog;
impl Pet for Dog {
    fn new_pet() -> Dog {
       Dog
    }
}

struct Cat;
impl Pet for Cat {
    fn new_pet() -> Cat {
        Cat
    }
}

fn set_pet(pet_holder: &mut Option<dyn Pet>) {
    //I wanted to say this
    //pet_holder.new_pet()
    //but I think this is more intuitive although requires more compiler magic
    *pet_holder = Some(pet_holder.new_pet());
}
fn choose_from_menu(options: Vec<&mut Option<dyn Pet>>) -> &mut Option<dyn Pet> {
     options[0]
}

fn main() {
    let cat = None as Option<Cat>;
    let dog = None as Option<Dog>;
    let pets = vec![&mut cat, &mut dog];
    let pet = choose_from_menu(pets);
    set_pet(pet);
}

//the derived method
fn new_pet_derived(self: &mut Option<Self>) {
    //Simply delegate to the static method
    //After monomorphization this should use the method from the concrete type
    *self = Some(Self::new_pet());
}

```

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 5, 2018, 4:52am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/8 "2018-07-05T04:52:52Z")

</div>

ZSTs can help on making static functions virtual, in other languages this is called the Monostate pattern (or slightly different, Singleton, which can have internal states whilst Monostate is constant).

The problem is that they require all methods of a specific type to be static or object state irrelevant. In other words, I want to put some object state aware methods as well as the irrelevant ones, and put this fact in the method signature, so the caller only need to check the signature to ensure the implementation does not even refer to the object data.

Also, I never see that the unused pointer field for ZST being a problem. Instead, I want to USE this field as a pointer to work on, without assuming an object is already initialized (this is the case when `&mut Option<dyn Trait>` points to a `None`).

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [July 5, 2018, 8:05am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/9 "2018-07-05T08:05:24Z")

</div>

> [@earthengine](#):
>
> The idea of “static virtual function” is to do dynamic dispatch depending on the concrete type, not the value. A classic example is the “virtual constructor”: You want to create an instance of a `Pet` , but until runtime (the user selection) you don’t know whether you should create a `Cat` or a `Dog` .

Okay, this sort of use case I'm familiar with, but I don't see why any `Option` magic is required or even beneficial for it. You can already _statically_ dispatch on the type with a generic constructor like `fn<T: Pet> new_pet() -> T { return T::new(); }`, or "dynamically dispatch" on far more ordinary values like an `enum PetType { Cat, Dog }` or `bool: must_support_barking` or whatever. Why do you want this new magic `Option` style of virtual constructor?

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 5, 2018, 8:13am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/10 "2018-07-05T08:13:18Z")

</div>

The reason not doing with an `enum` or `bool` etc, is of cause `trait` is open and `enum` is closed. One can implement another type of strange `Pet`

```rust
struct Spider{}
impl Pet for Spider{...}

```

without having to change the crate that defines `Pet` (as an enum).

Yes, in type theory, traits (viewed as “intersection type”) are very close to an `enum` (sum type). But for code organization (software engineering) they serves for different purposes.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [July 5, 2018, 8:26am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/11 "2018-07-05T08:26:37Z")

</div>

> [@earthengine](#):
>
> The reason not doing with an `enum` or `bool` etc, is of cause `trait` is open and `enum` is closed.

What about static dispatch?

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 5, 2018, 8:28am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/12 "2018-07-05T08:28:15Z")

</div>

Static dispatch is not in consideration. I know it is possible already, but it is not “dynamic” enough.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [July 5, 2018, 8:45am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/13 "2018-07-05T08:45:10Z")

</div>

It seems like an alternative that avoids doing anything special with `Option` would be some form of “class-safe” traits (to borrow the corresponding OOP to “object-safe” traits). Taking your earlier example it could be something like:

```rust
trait Pet {}

// "class-safe" because it only has static methods that don't return `Self`
trait NewPet {
    fn new() -> Box<dyn Pet>;
}

struct Dog;
impl Pet for Dog {}
impl NewPet for Dog { fn new() -> Box<dyn Pet> { Box::new(Dog) } }

struct Cat;
impl Pet for Cat {}
impl NewPet for Cat { fn new() -> Box<dyn Pet> { Box::new(Cat) } }

fn choose_from_menu(options: Vec<class NewPet>) -> class NewPet {
     options[0]
}

fn main() {
    let pets = vec![classof(Dog), classof(Cat)];
    let pet_type = choose_from_menu(pets);
    let pet: Box<dyn Pet> = pet_type.new();
}

```

`classof(Dog)` would be something like a ZST for the struct type, coerced to `class NewPet` would be a ZST + vtable of the `NewPet` static methods.

Presumably there’s some much more appropriate terminology and syntax for this, and I’m not sure if anything like this is worthwhile, this was just my first thought on seeing your usecase.

EDIT: Looking at this again I realise it’s basically the factory pattern with some sugar. I guess part of what you want is to combine the `Pet` and `NewPet` traits into one? Which would require supporting slicing off the “class-safe” parts of the trait when defining the class vtable.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 6, 2018, 12:59am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/14 "2018-07-06T00:59:07Z")

</div>

> Which would require supporting slicing off the “class-safe” parts of the trait when defining the class vtable.

This is already exists in Rust. [Example](https://play.rust-lang.org/?gist=14457a91c003eca55431a8b242e049a5&version=stable&mode=debug&edition=2015):

```rust
trait NotTraitObj {
    fn do_something(&self);
    fn new() -> Self;
    fn do_somethingelse(&self);
}
trait TraitObj {
    //in vtable
    fn do_something(&self);
    //not in vtable
    fn new() -> Self where Self: Sized;
    fn do_somethingelse(&self) where Self:Sized;
}
struct MyObj();
impl TraitObj for MyObj {
    fn new() -> Self where Self: Sized {
        MyObj()
    }
    fn do_something(&self){}
    fn do_somethingelse(&self){}
}
impl NotTraitObj for MyObj {
    fn new() -> Self {
        MyObj()
    }
    fn do_something(&self){}
    fn do_somethingelse(&self){}
}

fn main() {
    let obj = MyObj();
    let to: &TraitObj = &obj;
    to.do_something();
    //error: the `do_somethingelse` method cannot be invoked on a trait object
    //to.do_somethingelse();
    //error[E0038]: the trait `NotTraitObj` cannot be made into an object
    //let nto: &NotTraitObj = &obj;
}

```

In short, if you declare a method with constraint `Self: Sized`, this method is not "virtual" and will not be in the vtable.

I didn't use this trick in my code above because 1) I am demostracting hyperthetical grammar; 2) I want to make it callable on trait objects.

---

<div class="post-metadata">

**Author:** ![earthengine](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/earthengine/32/2577_2.png) [@earthengine](https://internals.rust-lang.org/u/earthengine)\
**Post date:** [July 6, 2018, 3:40am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/16 "2018-07-06T03:40:02Z")

</div>

Another case that `&mut Option<dyn Trait>` might be useful is to handle the `Box<FnOnce>` issue.

```rust
fn main() {
    let c = ||{};
    let b :Box<dyn FnOnce()> = Box::new(c);
    b()
}

```

Today the above results in E0161: cannot move out of box. But we can image to have compiler support for desugaring it to:

```rust
fn main() {
    let c = ||{};
    let b:Box<dyn FnOnce()> = box c;
    //this only copies the vtable, and move the destructor from b to the place holder
    let _place_holder:&mut Option<dyn FnOnce()> = unbox_to_some(b);
    //fn call_once_derived(self: &mut Option<dyn FnOnce()>) 
    //{ (self.take().unsafe_unwrap())() }
    _place_holder.call_once_derived()
}

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/more-magics-on-option-t/7837/17 "2019-03-25T08:30:27Z")

</div>

Closed on March 25, 2019, 8:30am UTC

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