0

I have been using hash tables to return complex data from functions, and it has worked well, but I would like to have the keys strongly typed, since I have booleans, strings, arrays of strings, ordered dictionaries and such in the returned hash tables. So, given something like this

[hashtable]$hashtable = @{
    one = 1
    two = "two"
}

I have the issue that the type of each key is weakly typed. I want to basically do this

[hashtable]$hashtable = @{
    [int]one = 1
    [string]two = "two"
}

But that's not valid code. So I thought I could do this

[psCustomObject]$object = [psCustomObject]@{
    [int]one = 1
    [string]two = "two"
}

But that's invalid too. I find this a bit ugly, and it also doesn't work

$object = New-Object -typeName:PSObject
$object | Add-Member -memberType:int -name:'one' -value:1
$object | Add-Member -memberType:string -name:'two' -value:'two'

So, am I SOL and there is no way, or no elegant way, to create a custom object with strongly typed properties?

4
  • Maybe you are looking for DataTable, where you can set each column to contain a certain DataType?
    – Theo
    Jun 11, 2019 at 10:43
  • @Theo, I am not familiar with data tables, but given that my end goal is not formatting for output I am thinking that's not the solution. I want to able to have a function return a complex data structure, and I want to be able to strongly type the return object so that I catch any errors where I assign the wrong data type to a property in the function. Often the resulting data will never be used for anything other than a conditional in the calling code.
    – Gordon
    Jun 11, 2019 at 10:47
  • A DataTable is not for formatting output, but instead a structure to store data, just like a PSObject. The main difference is that with a DataTable, you define strongly typed data types on each 'field' (column) so it will not accept any other data type. There are plenty of examples of how to use it, like here for instance. Sounds to me that is exactly what you want.
    – Theo
    Jun 11, 2019 at 10:57
  • @Theo; I see now. It does look like I could do what I want, but at the expense of a much more convoluted initialization, as well as data referencing. I am starting to wonder if maybe the reason you can't do this easily with hash tables or PSCustomObject is because most people don't think there is a need to. Which makes me wonder if I am either structuring my functions badly or just worrying too much.
    – Gordon
    Jun 11, 2019 at 11:06

1 Answer 1

2

Inside the hashtable literal you'll want to type-cast the value expression instead:

PS C:\> $object = [PSCustomObject]@{
    one = [int]1
    two = [string]"two"
}
PS C:\> $object|gm -MemberType NoteProperty

   TypeName: System.Management.Automation.PSCustomObject

Name MemberType   Definition
---- ----------   ----------
one  NoteProperty int one=1
two  NoteProperty string two=two

This will, however, not prevent anyone from storing a non-integer or non-string in any of the properties - psobject property are simply not strongly typed.


If you want type safety for properties you'll need to create a new type with the class keyword:

class MyOneTwo
{
    [int]$One
    [string]$Two

    MyOneTwo(){}

    MyOneTwo([int]$one, [string]$two){
        $this.One = $one
        $this.Two = $two
    }
}

# Create instances with ::new(), New-Object or a cast:
$object = [MyOneTwo]::new(1,"2")
$object = New-Object MyOneTwo -Property @{ One = 1; Two = "2" }
$object = [MyOneTwo]@{ One = 1; Two = "2" }
2
  • that will set the type, but not strongly. I could always come back later and assign a string to the first property like $object.one = [string]'one'
    – Gordon
    Jun 11, 2019 at 10:51
  • @Gordon That would be the same if you used Add-Member -TypeName - psobject properties are simply not strongly typed - for that you'll need a class definition Jun 11, 2019 at 14:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.