Skip to content

Conversation

@0101
Copy link
Contributor

@0101 0101 commented Jun 29, 2022

#8962

So far it works for records and anonymous records. Would like to get it to work for regular structs too but they seem to use a different mechanism for generating their getters.

The following case is not covered, because that would require analyzing user supplied getter code for side-effects.

[<Struct>]
type Bar(x: int, y: int) =
    member _.Item1 = x
    member _.Item2 = y

It does work for when the getter is auto-generated:

[<Struct>]
type MyStruct =
    val MyField: int

@0101 0101 force-pushed the struct-getters-readonly branch from b9484c2 to 41035ab Compare June 29, 2022 17:33
@0101 0101 force-pushed the struct-getters-readonly branch from a19aa4a to 738184c Compare June 30, 2022 12:04
@0101 0101 changed the title [WIP] Auto-generated getters on structs get readonly attribute Auto-generated getters on structs get readonly attribute Jul 1, 2022
@0101 0101 marked this pull request as ready for review July 1, 2022 10:12
let ilMethName = "get_" + ilPropName
let access = ComputeMemberAccess isPropHidden
yield mkLdfldMethodDef (ilMethName, access, isStatic, ilThisTy, ilFieldName, ilPropType)
let isStruct = tcref.IsFSharpStructOrEnumTycon && not tcref.IsEnumTycon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why the check is implemented this way?
At least in the IL code something else seems to be analyzed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed like the easiest way to do it here. I'm definitely open to other suggestions. Maybe we should add IsFShartStructTycon. Or maybe just use IsFSharpStructOrEnumTycon because enum will never reach this place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving it up to you, just voting for consistent approach if applicable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do it like in ILTypeDef, but I did find another way which might be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I'm definitely okay with the current approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

The property of Struct Record should add [<IsReadOnly>] for the read operation

3 participants