Article
· Nov 19, 2018 2m read

Language features that you really should know from day 1

Here's a fun test to see how well you know ObjectScript.

What will each of the following statements output?

write 5 * 10 + 1

write 1 + 5 * 10

write 1 + 0 / 10

write 0 && 0 = 0

write 0 = 0 && 0

write 1 && 1 = 1

write 1 = 1 && 1

write 2 && 2 = 2

write 2 = 2 && 2

write "1 APPLE" + "2 BANANAS"

write "-1" + "1-2"

write +"+-+-+-5"

write "1D7P"-1

write "1E6F"-1

write 0 = "FOO"

write 0 = +"FOO"

write 0 = ''"FOO"

write "10X" - " 5" - "5"

write "10-5"

write +"10-5"

write @"10-5"

write 5_"1"-1

write "5"-1_"1"

write 10 + 10 > 21 + 1

Scroll down to see how many you got right...

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

   \/

>write 5 * 10 + 1
51
 
>write 1 + 5 * 10
60

>write 1 + 0 / 10
.1
 
>write 0 && 0 = 0
1
 
>write 0 = 0 && 0
0
 
>write 1 && 1 = 1
1
 
>write 1 = 1 && 1
1
 
>write 2 && 2 = 2
0
 
>write 2 = 2 && 2
1
 
>write "1 APPLE" + "2 BANANAS"
3
 
>write "-1" + "1-2"
0
 
>write +"+-+-+-5"
-5
 
>write "1D7P"-1
0
 
>write "1E6F"-1
999999
 
>write 0 = "FOO"
0
 
>write 0 = +"FOO"
1
 
>write 0 = ''"FOO"
1
 
>write "10X" - " 5" - "5"
5
 
>write "10-5"
10-5

>write +"10-5"
10
 
>write @"10-5"
5

>write 5_"1"-1
50

>write "5"-1_"1"
41

>write 10 + 10 > 21 + 1
1

There are a couple of things going on here that are important to understand, otherwise your logic is going to look like its doing unexpected things.

The first is that ObjectScript implements strict left to right precedence, that means ObjectScript is going to behave completely differently to other languages that you might use.

The second is the automated coercion of strings to numbers, this can be both useful and a trip hazard that you need to be aware of. I've also seen some cool and crazy ideas that leverage on this, but best stay away from doing anything too clever as it can lead to unreadable code.

Precedence and Coercion are the main points here, and if you are new to the language make sure you have a good handle on them. For more detailed information read the documentation here...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GCOS_operators

Discussion (13)4
Log in or sign up to continue

Pretty embarassing result for me. I misinterpretted how ObjectScript treats equals(=) operator when used with a write and thus lost a lot of marks.  Its never something I've come across but now I am well and truely informed. Good article. From a support perspective, I often have to evaluate expressions in the terminal to check my understanding is correct. It can be mighty fustrating if you spend all day assuming an expression is interpretted one way, only to find your assumption to be incorrect! Agree with Avoid ambiguous or easily misinterrepted code. Also use brackets, they are your friends.

Thanks Jolyon for that embarrassingly simple solution. Got my curiosity going though and I can now understand why I didn't know of this feature in the first place - the "Advanced" sub menu doesn't appear on my Edit menu in Studio 2012.1.2 (where I did a lot of my development) though the option for customising Keyword Expansion does appear in the Options. All good in subsequent versions though.