PowerShell class array of strings syntax - class

I have the following class, but instead of a [String] member variable I need to declare an array of Strings. What's the syntax?
class SomeClass{
[String] $patterns;
SomeClass(){
$this.patterns = "Maintenance Code","Page \d+ of";
}
[Bool]SomeMethod([string]$txt){}
}

So as PetSerAl said [System.String[]] but [String[]] is just fine.
It's also mentioned in help about_arrays.
To create a strongly typed array, that is, an array that can contain only
values of a particular type, cast the variable as an array type, such
as string[], long[], or int32[]. To cast an array, precede the variable
name with an array type enclosed in brackets. For example, to create a
32-bit integer array named $ia containing four integers (1500, 2230, 3350,
and 4000), type:
[int32[]]$ia = 1500,2230,3350,4000

Related

Swift, can an array literal containing integers be used interchangeably with an array of integers?

In my example I have a method accepting an IndexSet:
func printIndexSet(_ indexSet: IndexSet) {
indexSet.forEach {
print($0)
}
}
If I try to pass it an array literal containing integers, it can deduce its type and construct an indexSet:
printIndexSet([1, 2]) // Compiles fine
If I give it an array of integers though it will not compile
// The following fail with error:
// Cannot convert value of type '[Int]' to expected argument type 'IndexSet'
printIndexSet(Array<Int>([1,2]))
let indices: [Int] = [1, 2]
printIndexSet(indices)
what's happening here?
There is an important difference between types and literals in Swift.
As you said, [1, 2] is an array literal. Not an array. An array literal is basically something that can be used to create any type that conforms to ExpressibleByArrayLiteral.
You can use an array literal to create an array, but you can use it to create other types, like IndexSets.
With printIndexSet([1, 2]) you use your array literal to create an IndexSet.
And printIndexSet(Array<Int>([1,2])) gives you an error because your func is expecting an IndexSet as an argument not an Array.
Hope this helps!
Update:
As #rmaddy correctly pointed out in the comments below my answer, IndexSet conforms SetAlgebra, which conforms to ExpressibleByArrayLiteral. This is why you can use your array literal to create the IndexSet.

Replace "," in PSCustomObject's properties while retaining the object type

I have the following sample code which replaces all comma values (,) with a period (.):
$foo = [PSCustomObject]#{
Num1 = "0.11"
Num2 = "0,12"
}
Write-Host "Type before:" $foo.GetType().FullName
$foo = $foo -replace(",",".")
Write-Host "Type after:" $foo.GetType().FullName
It produces the following output:
Type before: System.Management.Automation.PSCustomObject
Type after: System.String
I'd like to retain the PSCustomObject type and not have it converted to a string. Any ideas on how to accomplish this aside from:
$foo.Num2.Replace(",",".")
I'd prefer not to get into listing out each property with a replace statement as my real code has MANY properties in this object.
PowerShell comparison operators (including the replacement operator) implicitly convert the first operand to a type that matches the second operand. In your case that is transoforming the custom object to a string representation of itself.
To replace something in the properties of your object: enumerate the properties and do the replacement operation on the actual properties, not the object as a whole.
$foo.PSObject.Properties | ForEach-Object {
if ($_.Value -is [string]) {
$_.Value = $_.Value.Replace(',', '.')
}
}

How to call a function with an `inout` argument on each element of array?

I have a function which takes inout argument
func modify(word: inout Word)
I need to call it on each element of array. Here is what I do
for word in words {
modify(word: &word)
}
But I get error:
cannot pass immutable value as inout argument: 'word' is a 'let' constant
I tried to iterate through map words.map{ modify(word:&$0) }, still the same error:
cannot pass immutable value as inout argument: '0$' is immutable
Is there any way to call a function with an inout argument on each element of array?
When using the for word in words syntax, word is actually an immutable copy of the element in the array.
To modify the array directly, iterate over the indices instead:
for i in words.indices {
modify(word: &words[i])
}
which is equivalent to (for arrays at least)
for i in 0..<words.count {
modify(word: &words[i])
}

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers?

Does TypeScript has variable names escaping feature like backticks in Scala for literal identifiers:
`0029-otherwise-illegal-scala-literal`
See Scala explanation in Need clarification on Scala literal identifiers (backticks)
You can find the spec at https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#8.2
Section 2.2.2 tells you
The PropertyName production from the ECMAScript grammar is reproduced
below:
  PropertyName:    LiteralPropertyName    ComputedPropertyName
  LiteralPropertyName:    IdentifierName    StringLiteral
   NumericLiteral
  ComputedPropertyName:    [ AssignmentExpression ]
A property name can be any identifier (including a reserved word), a
string literal, a numeric literal, or a computed property name. String
literals may be used to give properties names that are not valid
identifiers, such as names containing blanks. Numeric literal property
names are equivalent to string literal property names with the string
representation of the numeric literal, as defined in the ECMAScript
specification.
This includes string literals.
You can declare a property as a string literal:
class MyClass {
"return" = 1;
}
you can access it with square brackets
let myinstance = new MyClass()
let one = myinstance["return"]

Printing &self in swift, cannot assign to immutable value of type

This is just an exercise in pointers in swift, but I was trying to write a function that would print the pointer to self, but I kept getting an error "cannot assign to immutable value of type C". Is this something that is even possible in Swift?
class C {
static var c = C()
var a = 1
func printMyPointer() {
printPointer(&self) //cannot assign to immutable value of type C
}
func printPointer(ptr:UnsafePointer<C>) {
print(ptr)
}
}
C.c.printMyPointer()
As already explained in the other answer, you cannot pass a constant
as the argument for an in-out parameter. As a workaround, you can
pass an array, as this will actually pass the address of the first
element:
func printMyPointer() {
printPointer([self])
}
But even simpler, use unsafeAddressOf():
func printMyPointer() {
print(unsafeAddressOf(self))
}
Update for Swift 3: As of Xcode 8 beta 6, unsafeAddressOf
does not exist anymore. You can convert self to a pointer:
print(Unmanaged.passUnretained(self).toOpaque())
or
print(unsafeBitCast(self, to: UnsafeRawPointer.self))
From the Swift Docs:
You can only pass a variable as the argument for an in-out parameter. You cannot pass a constant or a literal value as the argument, because constants and literals cannot be modified. You place an ampersand (&) directly before a variable’s name when you pass it as an argument to an inout parameter, to indicate that it can be modified by the function.
Since self is immutable, you can't pass it as an inout parameter. Even though the method signature dosen't use inout, Swift treats pointer parameters like inout, according to the Swift blog:
Swift allows pointer parameters to be treated like inout parameters, so you can pass a reference to a var as a pointer argument by using the same & syntax.