Skip to content

Member-access enumeration works with setter methods, but not with property assignments #5271

Closed

Description

You can use member enumeration to get property values from all members of a collection, but you cannot currently set them (to a given, single value).
By contrast, you can use a setter method to do so.

# Create 2 temporary files and collect their [System.IO.FileInfo] representations
# in an array.
$files = (New-Item -Type File -Force '/tmp/t1.txt'), (New-Item -Force -Type File '/tmp/t2.txt')

# Using member enumeration, try to set a property on all elements of the array
# to the same value:

# Try to to *assign to a property*.
# !! FAILS with error "The property 'LastWriteTime' cannot be found on this object."
$files.LastWriteTime = (Get-Date).AddDays(-1)     

# Try the property's *setter method* - this works.
$files.set_LastWriteTime((Get-Date).AddDays(-1))

# Verify that the date was set to yesterday.
$files

Given that using the setter method works, I suggest also allowing the property-assignment form.
(Note that NoteProperty members, such as found on [pscustomobject] instances, do not have setter methods.)

If there are good reasons not to allow that, I suggest making the error message less confusing (if $someColl.foo finds property foo on the collection members, then $someColl.foo = ... should indicate that assignment is disallowed rather than claiming that the property doesn't exist).

Environment data

PowerShell Core v6.0.0-beta.9 on macOS 10.13
PowerShell Core v6.0.0-beta.9 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Activity

  1. markekraus commented on Oct 29, 2017

    @markekraus
    Contributor

    I hope this is by design. I don't want to imagine the horrors that await us all should this work: $users.DisplayName = 'John Doe'. To me, geting the properties as a collection isn't harmful (well to anything other than performance maybe), seting them is destructive. I'd want the language to make sure I really meant coercing the properties of all members of a collection instead of intending to do so on an individual item.

  2. mklement0 commented on Oct 29, 2017

    @mklement0
    ContributorAuthor

    Mark Kraus (@markekraus):

    One [hu]man's horror is another's convenience.

    I get that this can be tricky, but note that you can already be destructive in this manner with methods, and there is a distinct lack of symmetry in only allowing the get operation (which also carries the ambiguity of whether collection-level vs. member-level access was intended).

    If the consensus turns out to be that it shouldn't be allowed, the error message should be amended to say so.

  3. markekraus commented on Oct 29, 2017

    @markekraus
    Contributor

    Michael Klement (@mklement0) there is a huge difference between doing this with a method and doing it with a property. using a set accesor's direct method (which is usually hidden and not obvious) implies explicit intent. Assignment to a property of a collection is ambiguous just as get operations on the property of a collection are ambiguous. The former is destructive, the later is (usually) not. I would rather not have unsafe ambiguous operations.

    That lack of symmetry probably (I hope, at least) exists to protect users from themselves. I hope this never gets added even though it would be convenient for those who know what they're doing.

    I agree with you on the error message. It should be more informative.

  4. mklement0 commented on Oct 29, 2017

    @mklement0
    ContributorAuthor

    Mark Kraus (@markekraus):

    I agree about the difference with respect to the setter method vs. assignment in the context of property-value assignment.

    However, you can call any method with member enumeration, and there again you may get tripped up by not realizing that the method may be called on each member - and thus do unintended things.

    Take the following example:

    > $a = 1, 2; $a.Reset()
    Method invocation failed because [System.Int32] does not contain a method named 'Reset'
    ...

    Say you thought that arrays have a .Reset() method and you wanted to call it on the whole array, but since they don't, .Reset() is called on each array element. No harm done in this case, because the [int] elements happen to have no .Reset() method, but they certainly could.

    (Of course, you could now make the case that calling methods should be disallowed too, but note that that would be a breaking change.)


    As for the benefits of allowing property assignments with member enumeration:

    • Symmetry

    • Convenience (personally, I'd find it useful and it is indeed in the context of naively assuming that that's already possible that I discovered the problem).

    I'm happy to leave it at that and let others weigh in.

  5. iSazonov commented on Oct 29, 2017

    @iSazonov
    Collaborator

    Enumeration works with property for pipeline in last versions. I believe it is possible to implement this for assignment too.
    And I have a troubling ambiguity in Michael Klement (@mklement0)'s example.

  6. added
    WG-Language
    Issue-Discussion
    on Oct 29, 2017
  7. markekraus commented on Oct 29, 2017

    @markekraus
    Contributor

    Michael Klement (@mklement0) I still feel that a method is more explicit with the intent. When I make an assignment, I expect it to be a 1-to-one mapping.

    Here is something else that would be ambiguous:

    $collection.EnumeratedProperty = 1, 2, 3

    Would that set all collection elements to an array like this does?

    $a = 1, 2, 3

    or would it set the first 3 like this?

    $a, $b, $c, $d = 1, 2, 3

    And how would this behave?

    $a, $collection.EnumeratedProperty = 1, 2, 3
  8. lzybkr commented on Oct 29, 2017

    @lzybkr
    Contributor

    This is definitely by design. I'm a big fan of symmetry, but I'm a bigger fan of pragmatism.

    In this case - allowing method calls was pragmatic - yes you can mutate object or system state, and in some cases that's exactly goal, but methods also do other things.

    Assignment does one thing - and we felt strongly that assigning the same value to every instance was likely not expected or desirable. If it is, then calling the setter is an acceptable alternative.

    Now in hindsight, we maybe should have used a new operator (and to be clear, it was considered). If we used a different operator, then I'm fairly certain we would have opted for symmetry.

  9. added
    Resolution-By Design
    on Oct 29, 2017
  10. mklement0 commented on Oct 29, 2017

    @mklement0
    ContributorAuthor

    Jason Shirk (@lzybkr):

    Understood, but note that calling a setter method is not an option for NoteProperty members, which rather limits the usefulness of this alternative.

    A dedicated operator would also have avoided the existing ambiguity (as shown in my .Reset() example above, which I presume is what Ilya (@iSazonov) finds troubling).

    Given the resolution as by-design, I suggest:

    • amending the error message

    • documenting this decision (add the Documentation Needed label).


    Mark Kraus (@markekraus): It's a moot point now, but by limiting the magic to the LHS and documenting that fact, there would be no ambiguity.
    Of course, assigning the same value to all members is the less common scenario, but it does have its uses and is consistent with the current behavior.

  11. changed the title [-]Member enumeration works with setter methods, but not with property assignments[/-] [+]Member-access enumeration works with setter methods, but not with property assignments[/+] on Oct 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-By DesignWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions