# Skip typing \`self.\` each time in a method and instead use \`.\`

**URL:** <https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627>\
**Category:** Uncategorized\
**Created:** [October 26, 2022, 9:25pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627 "2022-10-26T21:25:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsbj](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@nsbj](https://internals.rust-lang.org/u/nsbj)\
**Post date:** [October 26, 2022, 9:25pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/1 "2022-10-26T21:25:25Z")

</div>

Can we refer to members inside methods using dot `.member` instead of writing `self.member` each time ?

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 26, 2022, 11:06pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/2 "2022-10-26T23:06:26Z")

</div>

This would be ambiguous, for various reasons.

For instance:

```rust
if cond {
    a()
} else {
    b()
}
.method();

```

Is that an if-else calling `a()` or `b()` followed by a call to `self.method()`, or is that a call to `.method()` on the result of the if-else? Today it's unambiguously the latter.

---

<div class="post-metadata">

**Author:** ![nsbj](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@nsbj](https://internals.rust-lang.org/u/nsbj)\
**Post date:** [October 26, 2022, 11:16pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/3 "2022-10-26T23:16:49Z")

</div>

no it is not ambiguous because it is not valid to write:

```rust
if cond {
    a()
} else {
    b()
}
self.method();

```

I don't want to interpret any dot to `self.` but only where `self.` is valid.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [October 26, 2022, 11:23pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/4 "2022-10-26T23:23:27Z")

</div>

[It is valid to write that.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8ee6f0afa29708efd8e910c19e346cd6)

---

<div class="post-metadata">

**Author:** ![nsbj](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@nsbj](https://internals.rust-lang.org/u/nsbj)\
**Post date:** [October 27, 2022, 2:17pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/5 "2022-10-27T14:17:21Z")

</div>

my wrong ! it is indeed valid if the result of `if` is `()` ! however not a blocking thing it can be still parsed as it is currently

---

<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:** [October 27, 2022, 10:44pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/6 "2022-10-27T22:44:35Z")

</div>

Rust requires brackets on `if` in part to avoid the "dangling else" ambiguity observed in other C-like languages. We shouldn't be deliberately adding new syntax in that we know is similarly ambiguous.

Ambiguity isn't just about the compiler producing a single parse tree. It's also about the humans reading the code. Javascript's syntax isn't ambiguous per the computer -- there's a single correct parse which all javascript parsers produce for any valid syntax. However, Javascript's ASI is commonly known to be problematically confusing. You don't need to use semicolons in Javascript because of it... until you do. You can break expressions onto multiple lines... until you can't because of ASI.

Ultimately, the small savings aren't worth the ambiguity and potential confusion for humans.

(It's even _more_ bad if you're suggesting that how code parses should in any way depend on types. This is a complete nonstarter.)

---

<div class="post-metadata">

**Author:** ![Neutron3529](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/neutron3529/32/6976_2.png) [@Neutron3529](https://internals.rust-lang.org/u/Neutron3529)\
**Post date:** [October 29, 2022, 2:27am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/7 "2022-10-29T02:27:21Z")

</div>

what about `..method()`?

the only ambiguous could be, `a..b` is a `std::ops::Range`, but `Range` is not callable, thus `NO_MATTER_WHAT..method()` won't compile.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [October 29, 2022, 2:42am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/8 "2022-10-29T02:42:42Z")

</div>

That just looks like a typo.

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [October 29, 2022, 3:23am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/9 "2022-10-29T03:23:38Z")

</div>

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c17a4f070fde127634b0f7542cdeec4f)**
>
> A browser interface to the Rust compiler to experiment with the language

---

<div class="post-metadata">

**Author:** ![Neutron3529](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/neutron3529/32/6976_2.png) [@Neutron3529](https://internals.rust-lang.org/u/Neutron3529)\
**Post date:** [October 29, 2022, 9:01am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/10 "2022-10-29T09:01:54Z")

</div>

It seems the only solution is write `self.method()` as `method()` directly, and raise an warning if we use `method` function.

if the `method` method is defined elsewhere, we might specific where it was defined.

examples:

```rust
//use mycrate::method; // it could be called by both mycrate::method and the following call.
fn method() -> i32 {
    42
}
struct Foo();
impl Foo {
    fn method(&self) -> i32 {
        0
    }
    fn associated() -> i32 { // associated functions, could also be called by associated() rather than Self::associated
        0
    }
    fn calling(&self) {
        println!("{}", method()); // raise an warning during compilng since this method is ambiguous, print 42 since it is better to call global function as a default behavior
        println!("{}", self.method()); // return 0, no warning is generated
        println!("{}", crate::method()); // return 42, no warning is generated
        println!("{}", associated()); // return 0, but raise an warning during compilng if this method could be ambiguous.
    }
}

```

Since people seldom wrote different method with same name, this syntax sugar might be sweet.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [October 29, 2022, 10:23am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/11 "2022-10-29T10:23:02Z")

</div>

This is a problem with C++ that I'm glad Rust didn't repeat.

`self.` serves well to clearly identify method calls, and calls without `.` refer to global methods. That helps make code modular and composable.

As an example, traits would otherwise introduce further ambiguities here:

```rust
fn func() -> i32 {
    0
}

struct S();
impl S {
    fn calling(&self) {
        println!("{}", func()); // This is unambiguous
    }
}

trait Trait {
    fn func(&self) -> i32;
}
impl<T> Trait for T {
    fn func(&self) -> i32 { // This should not make the above ambiguous.
        1
    }
}

```

Losing that kind of modularity and composability is not worth saving five characters on method calls.

---

<div class="post-metadata">

**Author:** ![afetisov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/afetisov/32/8508_2.png) [@afetisov](https://internals.rust-lang.org/u/afetisov)\
**Post date:** [October 31, 2022, 12:55am UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/12 "2022-10-31T00:55:22Z")

</div>

> [@Neutron3529](#):
>
> It seems the only solution is write `self.method()` as `method()` directly, and raise an warning if we use `method` function.

That's the kind of syntax saccharine which leads people in C++ and Java to encode the method vs function distinction via a Polish naming convention: `my_field`, `our_method()`. What's the point of removing the syntactic difference between functions and methods, only to make users invent their own syntax for it?

---

<div class="post-metadata">

**Author:** ![eugene2k](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eugene2k/32/3525_2.png) [@eugene2k](https://internals.rust-lang.org/u/eugene2k)\
**Post date:** [October 31, 2022, 12:46pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/13 "2022-10-31T12:46:06Z")

</div>

> [@Neutron3529](#):
>
> It seems the only solution is write `self.method()` as `method()` directly

An alternative solution here is to add a hotkey to the editor to add `self.` and use that, and I'm kinda partial to that as it's really annoying when I read C++ code and can't tell if it's calling the class' method or an external function.

---

<div class="post-metadata">

**Author:** ![nsbj](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@nsbj](https://internals.rust-lang.org/u/nsbj)\
**Post date:** [November 11, 2022, 2:23pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/14 "2022-11-11T14:23:38Z")

</div>

I don't know why this would be hard to read. A dot following a block (be it for, if, an ordinary block ...) is interpreted as a dot operator for the result of the block since rust rust blocks results in value this is the done today and I don't want to change. I remember seeing a language with statements similar to rust which uses this approach to refer to self members and methods but I don't remember its name.

---

<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:** [February 9, 2023, 2:23pm UTC](https://internals.rust-lang.org/t/skip-typing-self-each-time-in-a-method-and-instead-use/17627/15 "2023-02-09T14:23:54Z")

</div>

Closed on February 9, 2023, 2:23pm UTC

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