# What's your experience with using % for accessing object members?

**URL:** <https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642>\
**Category:** Language enhancement\
**Created:** [October 15, 2023, 10:25pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642 "2023-10-15T22:25:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [October 15, 2023, 10:25pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/1 "2023-10-15T22:25:10Z")

</div>

Hello everyone,

What is your experience with using the `%` operator for accessing an object’s data and procedure members?

_Apologies if my complaints seem rude, that is not my intention. I just didn’t have a good experience with using `%`, and want to voice my experience._

Instead of `%`, I configured my IDE to show it as either `->` or `::`

I personally don’t like `%`, mainly because it makes the code difficult for me to see.

It might differ from person to person, but I have a hard time finding the `%` character on screen as in,

`this%NumCells = abstractKey%CountTotalCells()`

Some devs use a space to make things more legible …

`this % NumCells = abstractKey % CountTotalCells()`

This is somewhat better, but makes us waste lots of white space, and also complicates things when we have other symbols in the equations such as

`this % NumCells = abstractKey % CountTotalCells() + oldCells * 2 + this % CountNewCells() `

I don’t know if others have faced the same problem, but since we need to have space around `%`, the equation becomes more spaced out, and makes it difficult for me to see.

Right now, I just simply use a regex search to replace all occurances of `%` with `->` like in C, so the code becomes more easier for me to read.

In neovim editor I can do this easily with the search and replace command `:%s/%/->/g`

With the changes, the code becomes for a moment …

`this->NumCells = abstractKey->CountTotalCells() + oldCells*2 + this->CountNewCells() `

I have also tried with another option `::` which looks very good to me, and I prefer it to be the best option for me.

`this::NumCells = abstractKey::CountTotalCells() + oldCells*2 + this::CountNewCells() `

This is a hack, and is not something that I would’ve wanted to do, but it’s necessary for me to being able to see the code better.

The changes can be reversed with a single key press of `u`, so they don’t change the code. In future I’m trying to use an overlay technique that would prevent modification of the text, and would simply show `->` or `::` on top of the `%` instead of replacing it.

I don’t have anything else to say, as this is just me sharing my own experience with using the symbol, and I don’t know if others also have similar experience to me, or not.

Thanks

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [October 16, 2023, 1:27am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/2 "2023-10-16T01:27:30Z")

</div>

```plaintext
autocmd FileType fortran syntax match FortranConceal '%' conceal cchar=→
autocmd FileType fortran inoremap <buffer> ; %

```

you can try this in vim/neovim .`→` is a digraph, `<C-k>->`

For other languages, `.` is a default one to get members, it is easy to press, but `%( <shift>5 )` not. So I map`;` to `%`.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [October 16, 2023, 2:33am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/3 "2023-10-16T02:33:17Z")

</div>

> [@aerosayan](#):
>
> I personally don’t like `%`, mainly because it makes the code difficult for me to see.

Can you configure your editor to change the color of that character?

In fortran, :: is already used for declarations (integer :: i), and . is already used to delimit operators (.and., .or., .my\_op.)

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [October 16, 2023, 3:11am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/4 "2023-10-16T03:11:25Z")

</div>

> [@aerosayan](#):
>
> What is your experience with using the `%` operator for accessing an object’s data and procedure members? …
> 
> Instead of `%`, I configured my IDE to show it as either `->` or `::`
> 
> I personally don’t like `%`, mainly because it makes the code difficult for me to see.  
> …

@aerosayan ,

This has been discussed often over the last few years on several forums but know `% part-ref` has been a feature of the Fortran language standard since the publication of the Fortran standard since the Fortran 90 revision back in 1991, over 30 years ago.

Thus an IDE option you are trying is your best bet in terms of anything immediate. Perhaps you can force some syntax highlighting with a bigger, bolder font?

The influence of `C++` implies the other reference option is `.` (period) but I am not sure that is any better in terms of visual distinction in an editor?  
 ![image](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/e/e746804c5514d4589216717f559e7af9da1751eb.png)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [October 16, 2023, 7:33am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/5 "2023-10-16T07:33:54Z")

</div>

@aerosayan that’s a long standing issue that I also have with Fortran. I created an issue with many ideas here:

- [Idea: Support dot (`.`) as an alternative to the `%` operator (#84) · Issues · lfortran / lfortran · GitLab](https://gitlab.com/lfortran/lfortran/-/issues/84) (we since moved [the issue](https://github.com/lfortran/lfortran/issues/199) to GitHub).

Some prior discussion at Discourse [here](https://fortran-lang.discourse.group/t/what-are-your-favorite-and-least-favorite-things-about-fortran/27/9) and [here](https://fortran-lang.discourse.group/t/what-are-your-favorite-and-least-favorite-things-about-fortran/27/5).

You are bringing some other good ideas to use either `->` or `::` both of which I also feel are more readable.

Now when LFortran has a full parser (beta quality), we can try some of these ideas and see if it is possible to implement it robustly. I think just using `.` is the best, but it’s a bit tricky to implement, see the issue.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 16, 2023, 8:03am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/6 "2023-10-16T08:03:17Z")

</div>

> [@aerosayan](#):
>
> It might differ from person to person, but I have a hard time finding the `%` character on screen as in,

Not a problem for me as the `%` character appears in a different color in my text editor (Kate in KDE).

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [October 16, 2023, 9:23am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/7 "2023-10-16T09:23:41Z")

</div>

Like many others I don’t like much `%` for accessing the components. However I don’t feel that `this->component` or `this::component` are much more readable than `this%component`. I mean, they are more readable, but not that much to me, and not as much as `this.component`. Moreover I’m not sure that `::` would not be ambiguous in some cases:  
`integer::a = 2`  
could interpreted classically as the initialization (+implied save) of an integer variable `a`, or as an assignment to the component `a` of an object `integer`. Yes, the latter would definitely be a bad practice, but the parser would have to disambiguate that.

I think that the change is worth the effort only if the dot is chosen. A way to solve the ambiguities would be through a directive, similar to `implicit`:

`component_separator dot except(intrinsic,.foo.,.bar.)`

at the beginning of a programing unit would mean that `.` would be interpreted as a component separator, except for the intrinsic operators (e.g. `.and.`) and for the user defined `.foo.` and `.bar.`operators. Other specifiers in `except()` could be `user` (all user defined operators) and `all` (all operators).

That way, it would be the full responsibility of the programmer to define the precedences and remove any ambiguity for the parser.

**Edit** : still, there is a difficulty… If `foo` is component , `a.foo.b` would be broken if a new intrinsic `.foo.` was added to the language at some point and if `except(intrinsic)` has been specified

---

<div class="post-metadata">

**Author:** ![gronki](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gronki](https://fortran-lang.discourse.group/u/gronki)\
**Post date:** [October 16, 2023, 9:44am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/8 "2023-10-16T09:44:46Z")

</div>

Anyone is free at any moment to define:

```auto
interface operator (.foo.)
! ...
end interface

```

And it has been used in a smart way in a couple projects I have seen. Therefore, sadly it seems that dot would be hard to implement in a way that does not clash with custom operators.

`%` sucks a little bit, you know what, in my opinion, sucks a lot? Inability to do [this](https://fortran-lang.discourse.group/t/error-the-leftmost-part-ref-in-a-data-ref-cannot-be-a-function-reference/4990/15). While percent is an aesthetics issue, the other largely limits usability of the language.

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [October 16, 2023, 10:04am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/9 "2023-10-16T10:04:26Z")

</div>

> [@certik](#):
>
> I think just using `.` is the best, but it’s a bit tricky to implement, see the issue.

I also think `.` is best, but if that doesn’t work, how do you feel about `..`?

I have noted down few of the ideas I had, and ordered them based on what I think would be the best to worst.

`..` is not used in Fortran (AFAIK), and it is extremely easy to type and see.

`..` also allows very compact notation, which is good for writing complex equations.

```fortran
    ! Object
    type(MyType) :: x

    ! Accessors (ordered from best to worst, in my opinion)
    x.y.z = 123
    x..y..z = 123
    x::y::z = 123
    x->y->z = 123
    x&y&z = 123
    x & y & z = 123

```

---

<div class="post-metadata">

**Author:** ![gronki](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gronki](https://fortran-lang.discourse.group/u/gronki)\
**Post date:** [October 16, 2023, 10:09am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/10 "2023-10-16T10:09:13Z")

</div>

`..` is used as a range in many languages (Rust etc.), so I would be against it. `x..y` looks too much like “range of numbers between x and y”.

Same about `&`, since it clearly is a reference for most people looking at it.

`obj%p%x(3)%result`  
`obj`p`x(3)`result`  
`obj::p::x(3)::result`  
`obj->p->x(3)->result`  
`obj.p.x(3).result` – no (can be confused with `(obj) .p. (x(3).result)`)

``` is my favorite alternative, but overall I do not think that much can be improved here, dot being out of the game.

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [October 16, 2023, 10:18am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/11 "2023-10-16T10:18:36Z")

</div>

> [@gronki](#):
>
> `..` is used as a range in many languages (Rust etc.), so I would be against it. `x..y` looks too much like “range of numbers between x and y”.

I think it would be okay to define semantics of a symbol in a way that clashes with other languages, as long as it solves a problem in the current language.

`..` definitely has other meaning in other languages, and so does `**` and `::`

In C++ `::` is used for scopes, but while coding Fortran we can understand that `::` is used to declare variables.

Similarly, I would assume that `..` will not cause confusion, if Fortran only uses it to access objects, and uses something else for defining ranges.

If both features need to be supported, then accessing objects needs to be more user friendly than defining ranges.

For defining ranges, I would support range(1,n) instead of `1..n`

---

<div class="post-metadata">

**Author:** ![gronki](https://avatars.discourse-cdn.com/v4/letter/g/a88e4f/32.png) [@gronki](https://fortran-lang.discourse.group/u/gronki)\
**Post date:** [October 16, 2023, 10:22am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/12 "2023-10-16T10:22:46Z")

</div>

It is not exactly a problem. There is no problem with `%` other than me and you do not like it. From the objective standpoint it works perfectly. Unlike many features in Fortran that are straight-up broken by design.

`::` makes much more sense, since it is used in a sort-of similar way in C++. But worth the effort? In my opinion, no.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 16, 2023, 10:54am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/13 "2023-10-16T10:54:24Z")

</div>

> [@gronki](#):
>
> But worth the effort? In my opinion, no.

I agree.  
And introducing a second syntax for the same thing would just add confusion in my humble opinion.

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [October 16, 2023, 11:41am UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/14 "2023-10-16T11:41:34Z")

</div>

> [@vmagnin](#):
>
> And introducing a second syntax for the same thing would just add confusion in my humble opinion.

Fortran does have many different syntaxes for the same thing.

The language itself changed from being fixed form to today’s modern free form.

At some point developer experience becomes crucial enough to allow improvement of the syntax and the language.

Although, yes, I agree that this modernization effort should be done in a conservative manner.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [October 16, 2023, 12:14pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/15 "2023-10-16T12:14:06Z")

</div>

> [@aerosayan](#):
>
> … I also think `.` is best …

@aerosayan ,

When you use " difficult to see" as a basis for your argument but veer toward `.`, which is quite difficult or worse for anyone facing actual physical visual challenges, then it is not much of an argument.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [October 16, 2023, 12:17pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/16 "2023-10-16T12:17:09Z")

</div>

> [@aerosayan](#):
>
> Fortran does have many different syntaxes for the same thing.
> 
> The language itself changed from being fixed form to today’s modern free form.

It’s true that I enjoy using the `[]` array syntax instead of the strange `(/ /)` 😀 (although I guess the second one is probably from a period where there was no `[]` on keyboards)

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [October 16, 2023, 12:35pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/17 "2023-10-16T12:35:08Z")

</div>

> [@FortranFan](#):
>
> When you use " difficult to see" as a basis for your argument but veer toward `.`, which is quite difficult or worse for anyone facing actual physical visual challenges, then it is not much of an argument.

Here’s an extremely zoomed view of a section of pseudo-code.

![code](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/8/8ee1caa3c4ba3cc01f42590de5e3b57878842021.png)

It is well known in typography world that we don’t read the words directly.

We read the difference in their height and width.

Even though `.` is the smallest of them all, it creates a difference in height that makes it easy to see. If it was difficult to see, then many famous languages like C, C++, Python, wouldn’t have used them, and the users would’ve complained about it for years.

Now, consider the `%` in `car%StartEngine()`…

It creates a block of visible imbalance around `r%S` that has no space or difference in height, which makes it the worst possible option for visible legibility.

Even though there’s a difference in height between `r` and `S`, the `%` in `r%S` completely hides it, and makes it difficult to see.

If those of us with good eye sight find `%` difficult to see, I think it’s fair to say that actually `%` is the worst for visibility, and `.` would be an improvement.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [October 16, 2023, 1:13pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/18 "2023-10-16T13:13:46Z")

</div>

> [@aerosayan](#):
>
> If it was difficult to see, then many famous languages like C, C++, Python, wouldn’t have used them, and the users would’ve complained about it for years.

No, there is **no** basis for this whatsoever, there is a better argument to be made for an implicit bias given the heavy influence of C-like languages where the C language basis carried forward but there is no evidence there was any **inclusivity** consideration toward the visually impaired in the design dating back at least to 1970s.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [October 16, 2023, 2:06pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/19 "2023-10-16T14:06:12Z")

</div>

> [@aerosayan](#):
>
> For defining ranges, I would support range(1,n) instead of `1..n`

`1:n` is already the syntax that defines ranges for array slices and in control structures like `do concurrent` and `where`.

I’d like to have it to generalized as in Matlab, i.e. `x = 1:n` would mean `x = [(i, i=1,n)]`

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [October 16, 2023, 2:19pm UTC](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642/20 "2023-10-16T14:19:06Z")

</div>

```fortran
this_element%foo_bar%val
this_element.foo_bar.val
this_element..foo_bar..val
this_element->foo_bar->val
this_element::foo_bar::val
this_element^foo_bar^val
this_element\foo_bar\val
this_element~foo_bar~val
this_element#foo_bar#val
this_element|foo_bar|val
...

```

I find that `a|b` does well

[Next page](https://fortran-lang.discourse.group/t/whats-your-experience-with-using-for-accessing-object-members/6642.md?page=2)
