Should we specify array type for OutputType and .OUTPUTS

Welcome Forums General PowerShell Q&A Should we specify array type for OutputType and .OUTPUTS

Viewing 3 reply threads
  • Author
    Posts
    • #267770
      Participant
      Topics: 2
      Replies: 5
      Points: 55
      Rank: Member

      Hello community, my first post here..

       

      Consider following function:

       

      I understand that entry array won’t be returned at once, the functionality of this sample function isn’t important at all.

      I also understand what OutputType is and does and it’s meaning is also not relevant here.

       

      My question is should we specify output as an array instead of just type name?

      I have some feeling (according to MSDN documentation) that we should specify just the type, and not quantity, for example for the above function this would be considered a conventional way to specify outputs?

       

      Which one of these 2 options to document outputs should we use to document code and if so, why?

      Why would specifying array types be wrong and is there anything “wrong” if we do so?

    • #267788
      Participant
      Topics: 5
      Replies: 170
      Points: 656
      Helping Hand
      Rank: Major Contributor

      I wouldn’t recommend specifying the output as an array unless it was some unique circumstance.  If someone wants to force your function to return an array regardless of output, they can use the array subexpression when calling the function i.e. @(Test-ArrayOutputType).

      For the most part, I rarely see the “return” keyword in PS functions, because generally functions in PS return objects at any point of execution and continue executing i.e.

      If you call this function, you will find it returns an array of 3 different objects even though an array is not implicitly instantiated inside the function definition.  If you pipe the function call to Get-Member, you will see the different object types.  Now if you put the “return” keyword in front of one of the first 2 objects, the function will stop execution at that point and not return the rest of the objects.  If however there is an object type your function must always return as an array, then I guess it would be ok to specify as such. i.e.

       

    • #267794
      Participant
      Topics: 2
      Replies: 5
      Points: 55
      Rank: Member

      That was very useful answer!

      I just couldn’t find confirmation to this, what was suspicious is that nowhere did I see output specified as an array, so it makes sense.

       

      I know the return keyword stops executing something at some point, It’s my left over from other languages 🙂

       

      Btw. I can’t see anywhere to upvote you, but thanks!

    • #268598
      Senior Moderator
      Topics: 9
      Replies: 1420
      Points: 5,280
      Helping Hand
      Rank: Community MVP

      OutputTyps attribute is not going to restrict the function in any terms. If its mentioned it will help in intellisense in the editors/console and of course as Mike mentioned, anytime any object can be returned.

      and both can be used for better readability and documentation purpose.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.