# Dot notation for "Methods"

**URL:** <https://discourse.elm-lang.org/t/dot-notation-for-methods/8727>\
**Category:** Request Feedback\
**Created:** [October 23, 2022, 10:33am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727 "2022-10-23T10:33:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anderssorby](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/anderssorby/32/2859_2.png) [@Anderssorby](https://discourse.elm-lang.org/u/Anderssorby)\
**Post date:** [October 23, 2022, 10:33am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/1 "2022-10-23T10:33:58Z")

</div>

> <https://github.com/elm/compiler/issues/2282>
>
> I think adding a function "method" dot notation would simplify expressions and e…ase adaptation for JS-developers.
> Lean4 is purely functional, but also has support for .dot notation for associated functions. https://leanprover.github.io/lean4/doc/namespaces.html
> I think this should be added to Elm too.
> 
> \## Example
> \`\`\`elm
> type alias A =
> { b: String }
> \-- this is possible
> getB = .b
> \-- A new possibility to add functions to the type "namespace"
> A.doStuff : A -\> B -\> C
> A.doStuff self b = c
> 
> \-- This could also be possible
> fun : A -\> B -\> C
> fun a = a.doStuff
> 
> f : Maybe a -\> b
> f m = m.map (\\a -\> a.stuff) |\> .withDefault defaultB
> \`\`\`

By inspiration from the Lean4 language I suggest a dot notation for “methods”.

---

<div class="post-metadata">

**Author:** ![dta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dta/32/1917_2.png) [@dta](https://discourse.elm-lang.org/u/dta)\
**Post date:** [October 23, 2022, 2:51pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/2 "2022-10-23T14:51:18Z")

</div>

I don’t see this as adding much value, and it complicates the language.

The convention in Elm is to make the “main object” the last parameter and use `|>`. Is there really a need to add `a.doStuff arg` when we already have `a |> doStuff arg`?

---

<div class="post-metadata">

**Author:** ![Anderssorby](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/anderssorby/32/2859_2.png) [@Anderssorby](https://discourse.elm-lang.org/u/Anderssorby)\
**Post date:** [October 24, 2022, 8:31am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/3 "2022-10-24T08:31:18Z")

</div>

I guess. I like the pipe syntax. Lean4 has both the |\> and . notation, but it’s use cases are different. The main point is perhaps the point of “namespacing” inside modules. Since you could have several A.doStuff and B.doStuff, and the compiler could figure out which to use based on the type name. One can ofc just write the module name right now, but in my code I have many long type names that would be nice to not always have to write.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [October 24, 2022, 8:39am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/4 "2022-10-24T08:39:24Z")

</div>

> [@Anderssorby](#):
>
> One can ofc just write the module name right now, but in my code I have many long type names that would be nice to not always have to write.

```auto
import MyVeryVeryLongNamedModuleForCats as Cats

```

---

<div class="post-metadata">

**Author:** ![Anderssorby](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/anderssorby/32/2859_2.png) [@Anderssorby](https://discourse.elm-lang.org/u/Anderssorby)\
**Post date:** [October 24, 2022, 12:16pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/5 "2022-10-24T12:16:28Z")

</div>

@rupert Yes, I know. I might be just lazy, but I would want that convenience sometimes.

---

<div class="post-metadata">

**Author:** ![dta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dta/32/1917_2.png) [@dta](https://discourse.elm-lang.org/u/dta)\
**Post date:** [October 24, 2022, 8:43pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/6 "2022-10-24T20:43:12Z")

</div>

Allowing more than one symbol with the same name but different types does seem like a feature that could be nice, although I can also see some people thinking it could make code harder to reason about. That change does not need to be coupled with the new syntax, though.

---

<div class="post-metadata">

**Author:** ![nil](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nil](https://discourse.elm-lang.org/u/nil)\
**Post date:** [October 24, 2022, 8:50pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/7 "2022-10-24T20:50:45Z")

</div>

I like the dot notation for the “auto-completion” features it provides in other languages editor tools.

The pizza requires two special character keypresses, both requiring the shift key, and never “auto” completes based on the context from the left side of the pizza.

```auto
import List
[1, 2, 3] |> List.first
--vs
Import List
[1, 2, 3].first 

```

(Implicitly uses the “first” from the list module, if its the only matching imported function. Could match on imported method that takes a list as the final argument.) Could maybe work something like [F# extension methods](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/type-extensions#extension-methods).

Would be nice, but as others say I guess there are good reasons for not including it. I’m not a language designer. I like the question and suggestion though!

---

<div class="post-metadata">

**Author:** ![Tanneguy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/tanneguy/32/3387_2.png) [@Tanneguy](https://discourse.elm-lang.org/u/Tanneguy)\
**Post date:** [October 25, 2022, 7:43am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/8 "2022-10-25T07:43:12Z")

</div>

Philosophy : You Write Code For Others

The total time of code management includes

- writing & modifying
- reading & understanding

In my experience the second component is where you lose time and money unless in pet projects.

Now let’s say we meet an expression `x.first` somewhere in the code, it can mean either `x : List a` or `x : {first : b}`, and we need to go read where `x` was initialized.

This breaks the flow of reading the code, and in the end would be very detrimental to code maintenability - which is by far my favorite Elm feature.

---

<div class="post-metadata">

**Author:** ![mattpiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattpiz/32/860_2.png) [@mattpiz](https://discourse.elm-lang.org/u/mattpiz)\
**Post date:** [October 25, 2022, 12:41pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/9 "2022-10-25T12:41:50Z")

</div>

I do like that most IDEs know how to auto complete methods. I hope the elm language server will know at somepoint how to autocomplete after seing a `|>` by looking for functions in scope with the same type for their last argument.

---

<div class="post-metadata">

**Author:** ![lpil](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lpil/32/3139_2.png) [@lpil](https://discourse.elm-lang.org/u/lpil)\
**Post date:** [October 27, 2022, 4:30pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/10 "2022-10-27T16:30:55Z")

</div>

> [@nil](#):
>
> I like the dot notation for the “auto-completion” features it provides in other languages editor tools.
> 
> The pizza requires two special character keypresses, both requiring the shift key, and never “auto” completes based on the context from the left side of the pizza.

The autocompletion functionality is a property of the editor/LSP, not the language here. There’s no reason `|>` couldn’t have type assisted autocompletion, it just hasn’t been implemented.

---

<div class="post-metadata">

**Author:** ![allanderek](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/allanderek/32/1360_2.png) [@allanderek](https://discourse.elm-lang.org/u/allanderek)\
**Post date:** [October 28, 2022, 2:18pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/11 "2022-10-28T14:18:54Z")

</div>

Show us some code that you have, or have found, that you think would be improved by a “dot notation for methods”. Show us the updated code which uses the new feature and explain what advantages you think it has, and any disadvantages. Your argument is then that this kind of code occurs often enough that the benefits (advantages - disadvantages) outweigh the cost of increasing the complexity of the language.

If your example code already exists so much the better. It could exist in standard or heavily used libraries, [or well presented examples](https://github.com/rtfeldman/elm-spa-example). This demonstrates well that the code situation you are describing occurs naturally, and you’re not making up a contrived example.

I find that whenever I consider a new language feature, a first step is looking for code that would be improved by such a feature, and I often find I’m struggling. In that case, it’s going to be a very hard struggle to convince anyone. You would have to demonstrate that such code doesn’t exist **because** the feature doesn’t exist and so developers are working around that at a higher level.

---

<div class="post-metadata">

**Author:** ![jhbrown](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jhbrown/32/3553_2.png) [@jhbrown](https://discourse.elm-lang.org/u/jhbrown)\
**Post date:** [October 28, 2022, 11:48pm UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/12 "2022-10-28T23:48:17Z")

</div>

One of the things I like about Elm is that it does NOT have dot methods, because the way they are used in many languages creates a lot of ambiguity: if I see `foo.bar` in Python, I do not know from context if `bar` is a field on this specific instance of `foo`, or a method associated with whatever type `Foo` is.

So if nothing else, I would ask that if there ever is a way to name a `method` by association with a variable, it uses something other than `.` to do it so that no new ambiguity is introduced.

(Even in Elm, there are a lot of cases – because of modules, and because `Foo.Bar` in a type signature is a type, but `Foo.Bar` in any other context is a reference to a constructor named `Bar` in the module named (or aliased) to `Foo`… no need to add more!)

---

<div class="post-metadata">

**Author:** ![turboMaCk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/turbomack/32/355_2.png) [@turboMaCk](https://discourse.elm-lang.org/u/turboMaCk)\
**Post date:** [November 6, 2022, 11:51am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/13 "2022-11-06T11:51:32Z")

</div>

I also left some comment in the original issue. Anyway maybe it’s worth also asking here. Perhaps I’m missing something but I wonder…

With this proposal I wonder what you would expect for this code to produce:

```elm
adding_one t =
  t.map (\a -> a + 1)

```

## My take

I can imagine primarily 2 expected results…

### Ad-hoc Polymorphism

This code should compile and work so that I can call:

```elm
adding_one (Just 1)
--> Just 2

adding_one [1, 2, 3]
--> [2, 3, 4]

```

Anyway as you can see this introduces new type of polymorphism which is currently not possible in elm. The type would be something like:

```elm
adding_one : f Int -> f Int

```

where `f` is of kind `kind f = Type -> Type` (imaginary kind annotation since at the moment elm doesn’t have any).

### This Code should be rejected

Compiler should error with something `can't specialize f in ...` prohibiting you from using this feature in this way. In such case you would need to somehow specify which type does `t` have like:

```elm
adding_one : Maybe Int -> Maybe Int

```

Anyway as you can see this syntax would break type inference in a way it would no longer be possible to infer type without this annotation.

Also in my opinion this makes whole feature kind of useless. It would make `.` just an alternative syntax for `|>` which (unlike existing `|>`) is also ambiguous. This code which is already possible at the moment doesn’t have this problem and provides exactly the same capabilities:

```elm
adding_one f =
  f |> Maybe.map ((+) 1)

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [November 16, 2022, 11:51am UTC](https://discourse.elm-lang.org/t/dot-notation-for-methods/8727/14 "2022-11-16T11:51:55Z")

</div>

Closed on November 16, 2022, 11:51am UTC

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