fsharp / fsharp/fslang-suggestions

Allow d["a"][0].a

Open
#1,109 4 comments 12 reactions 0 assignees View on GitHub
approved-in-principle area: syntax
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

# Allow dotting into successive applications of the short indexer syntax

I propose we allow
```fs
let d = Unchecked.defaultof>>
d["a"][0].a // This value is not a function and cannot be applied.
```

The existing way of approaching this problem in F# is `(d["a"][0]).a`

https://github.com/dotnet/fsharp/issues/12549#issuecomment-1004818851

> in F# 6 there is no rule to allow the alternative interpretation as a left-associative series of index lookups for this case. Thus for compatibility reasons the parentheses are required.

## Pros and Cons

The advantages of making this adjustment to F# are
- Following the incentive of https://github.com/fsharp/fslang-design/blob/main/FSharp-6.0/FS-1110-index-syntax.md#motivation
> This is to make learning F# easier (less to learn) and allow simpler migration of existing code from Python, C# and other languages.
- Being concise and not writing unnecessary dots

The disadvantage of making this adjustment to F# is losing compatibility.

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: https://github.com/dotnet/fsharp/issues/12549

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:
* [ ] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the linked F# 6.0 index-syntax design and related issue #12549 to understand the current parsing rule and compatibility concern. Define the implementation and testing scope needed for d["a"][0].a to work as successive index lookups without breaking existing code.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.