jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Record access simplifications #162

Closed jfmengels closed 11 months ago

jfmengels commented 12 months ago

We already have

{ field = 1 }.field
--> 1

but we don't have

.field { field = 1 }
--> 1

I think we can take the same simplifications done in #161 for tuples and apply them for records where we currently don't

(✅ indicates this is now supported)

-- already shown above
.field { field = a } ✅
--> a

.field { record | field = a } ✅
--> a

.field { record | otherField = a } ✅
--> record.field

.field << (\a -> { field = b, other = other }) ✅
--> (\a -> b)

.field << (\a -> { record | field = b, other = other }) ✅
--> (\a -> b)

.field << (\a -> { record | otherField = a }) ✅
--> .field << (\a -> record)

(.field >> .bar) << (\a -> { record | otherField = a }) ✅
--> (.field >> bar) << (\a -> record)

-- Not this one, unless we want to inline `.field`, in which case we could do
.field << (\a -> if condition then { field = b, other = other } else { record | otherField = a })
--> (\a -> if condition then b else record.field)

And then if we the names and positions of record fields of type aliases, we can do the exact same simplifications done in #161 for tuples when using the automatic record constructor.

type alias Record = { first : Int, second : Int }

(Record a b).first ✅
--> a

.first (Record a b) ✅
--> a

.second (Record a b) ✅
--> b

.second << Record a
--> identity

.first << Record a
--> always a
lue-bird commented 11 months ago

These 2 don't make sense to me

.field << (\a -> { field = a, other = other })
--> a

.field << (\a -> { record | field = a, other = other })
--> a

I think both should return (\a -> a) (or identity), right?

jfmengels commented 11 months ago

Right! 👍 Probably the lambda version, because that applies to more situations.

Oh, and we should also support the check for all branches, like we already do with the non-function version of record access.

jfmengels commented 11 months ago

@lue-bird Shall I move the proposals for type alias Record = { first : Int, second : Int } into a separate issue?

lue-bird commented 11 months ago

Oh, I forgot about these. Reopened!

To me these seem very obscure, tho, so I'll focus elsewhere.