1.getType() vs "1".getType()

Hey there, i am complete newbie here. Please put some light on following:

“1”. getType() works

1.getType() gives error. We need parenthesis like (1).getType()

1.1.getType() works well

When you need parenthesis in a expression

Thanksa lot

 

The Powershell parses has a couple modes, Expression (basic math) and Command. You can see some examples here:

https://devblogs.microsoft.com/powershell/issues-with-windows-powershell-syntax/

When you type a 1, it assumes you are trying to perform arithmetic and errors saying it wants an expression (e.g. +,-. etc.). Parenthesis is basically a wrapper to do “this” first with PEMDAS, so you then able to basically wrap the expression and then use Get Type. The other example using double qoutes is taking you out of Expression mode and into Command mode because you cannot perform arithmetic strings and would be doing concatenation. The parser is just trying to understand what you are trying to do. Parenthesis is a group operator, which you can see more here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7

PS C:\Users\rasim> 1.GetType()
At line:1 char:11
+ 1.GetType()
+           ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression
 

PS C:\Users\rasim> (1).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                      
-------- -------- ----                                     --------                                                                                                                                                                      
True     True     Int32                                    System.ValueType                                                                                                                                                              

#Another example of PEMDAS, finish the expression and the get the type
PS C:\Users\rasim> (1 + 2).GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                      
-------- -------- ----                                     --------                                                                                                                                                                      
True     True     Int32                                    System.ValueType   

#Convert to a string and get the type
PS C:\Users\rasim> "1".GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                                                                      
-------- -------- ----                                     --------                                                                                                                                                                      
True     True     String                                   System.Object                                                                                                                                                                 

#Defining the numbers as strings results in concatenation, not addition arithmetic of 1+1 = 2 
PS C:\Users\rasim> "1" + "1"
11

#Just typing a letter, the error shows Powershell is in expression mode trying to understand the task
PS C:\Users\rasim> a.GetType()
At line:1 char:11
+ a.GetType()
+           ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression
 
#The parenthesis here take you out of Expression mode in Command mode, so it's looking for a command or alias or something defined as "a"
PS C:\Users\rasim> (a).GetType()
a : The term 'a' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:2
+ (a).GetType()
+  ~
    + CategoryInfo          : ObjectNotFound: (a:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

thanks a lot. great information.

if you could say something about 1.1.GetType()

is it in expression parser-mode?

My assumption is that the first dot (.) is in expression mode as you would do decimal arithmetic, but as soon as you hit dot (.) again it would assume you are looking for a property or method as math operations are a single decimal.

Ok. Thanks