Saturday, 29 September 2012

Undocumented feature - assignment files

I was asked yesterday if I knew about the Uniface function $assignments.  I did and it's something that we started using as soon as it became available (from version 9.something) and it's incredibly useful.  I searched the manuals so that I could send the enquirer the details, but I couldn't find any reference to it.

When a userver starts up, it reads the assignment file in order to get numerous settings.  There are many sections, including a custom section called "logicals", which allows you to add your own assignment files settings, which can be read using the $logical function.  

It is now possible to list all of the logicals at once, like this...

  list = $assignments("LOGICALS")

This returns a Uniface list of all the logicals.  It is possible to return any section of the assignment files in this way, including "paths", "files" and "services_exec".

This was mentioned on the Hacking UnifAce blog over a year ago, but hopefully you find it useful.

Summary: You can access the assignment file settings in code, listed by their section, which can be useful for checking which settings the current userver is using.

Thursday, 27 September 2012

Variables as lists

I think you'd be hard pushed to find a Uniface developer that didn't know that a string variable can be used to hold a list of values.  The Uniface list construct uses a gold-semi-colon as the list delimiter, denoted in the manuals as ; but in code looks like ·; - for example...

  s = "27/Sep/2012·;28/Sep/2012·;29/Sep/2012"

I spent quite a long time today trying to work out why such a simple thing was not working for me.  It took quite a long time for me to realise that I'd accidentally declared (or rather, re-used) a numeric variable.  This gives a rather different result...

  n = "27/Sep/2012·;28/Sep/2012·;29/Sep/2012"

I wasn't previous aware that a numeric variable could hold a list in this way, but it's handy to know! 

The same also works with dates...

  d = "27/Sep/2012·;28/Sep/2012·;29/Sep/2012"

Take a look at this screenshot to see the results...

I'm not going to go through all the data types to confirm which ones it works with, but I suspect it would work with all of the simple ones (such as datetime and float) but not with the more complex ones (such as handle and occurrence).

Summary: It's not just string variables that can be used to hold lists; numeric and date variables also can, along with many others I suspect.

Wednesday, 19 September 2012

Splitting long lines of code

In javascript, for example, white space is pretty much ignored by the interpretor.  You can spread your code over as many lines as you think makes it more readable, just don't forget to indicate the end of the line with a semi-colon.  However, Uniface doesn't allow this, it uses the carriage return as the end of line character.  So what do you do if you have a really long line of code?

Well, for me, I tend to just leave it as a very long line of code.  If it gets too long, I'll ask my boss for an even bigger monitor :) 

I suffer a little with this practice when I'm developing on my laptop though, as I was yesterday.  This reminded me about something that I've seen used before, and that's using a Uniface feature that allows you to split a single line of code over multiple lines, like this...

  if ( value != "1" & value != "2" & value != "3" & %\
       value != "4" & value != "5" & value != "6" & %\
       value != "7" & value != "8" & value != "9" )
    value = "0"

I'm sure this is something that's documented, but what do you search for to find it?  I certainly couldn't find anything.  So here's a little tip for you, if you prefer to keep those lines a little shorter.  

One situation I have used it in before is with a long SQL command or selectdb statement, as it can help to format it in a similar way to how you might write the SQL in your favourite database tool.  For example...

  selectdb max(field6) from "entity" u_where ( %\
    (field1 = value1) & %\
    (field2 = value2) & %\
    (field3 = value3) & %\
    (field4 = value4) & %\
    (field5 = value5)) to (value6)

EDIT:  Uniface calls this the "Line Continuation Marker" and it is documented as such. 
If anyone knows where this is in the manuals, please leave a comment and let me know.

Summary: It is possible to split a single line of code over multiple lines in Uniface, but you have to do it explicitly.  

Monday, 17 September 2012

Order of operations - part two

It's been pointed out to me that I overlooked a whole page in the Uniface manuals which details the order of precedence for operators - how did I miss that?  Luckily it agrees with my findings, and adds in a few of the Uniface specific functionality (such as indirection)...

Field Indirection
{ }
Struct Dereference and Operation Activation
Struct Index
[ ]
Indirect dereference
Dereference with string substiution
Arithmetic Operators
Relational Operators
Less than
Less than or equal to
Not equal to
Equal to
Equal to
Greater than or equal to
Greater than
Logical Operators
Logical NOT
Logical AND
Logical OR

Thanks to Dave R for pointing this out to me.

Summary: Check the manuals before spending hours writing a blog post :)

Order of operations

I was recently asked about the order of operations (or "operator precedence") in Uniface, and I couldn't really answer the question with an certainty, so I thought I'd investigate and prove or disprove my assumptions.  It relates primarily to mathematical expressions, but also to logical conditions, and defines in what order the various components are processed. 

Take for example the following mathematical expression...

x = 10 * 5 + 1

Operator precedence states that multiplication is done before addition, therefore x is 51, not 60.

Wikipedia states that the following order of operations should be true for most programming languages, so my question is, does Uniface comply?

1()   []   ->   .   ::Grouping, scope, array/member access
2 !   ~   -   +   *   &   sizeof   type cast ++x   --x  (most) unary operations, sizeof and type casts
3*   /   %Multiplication, division, modulo
4+   -Addition and subtraction
5<<   >>Bitwise shift left and right
6<   <=   >   >=Comparisons: less-than, ...
7==   !=Comparisons: equal and not equal
8&Bitwise AND
9^Bitwise exclusive OR
10|Bitwise inclusive (normal) OR
11&&Logical AND
12||Logical OR
13 ?:Conditional expression (ternary operator)
14=   +=   -=   *=   /=   %=   &=   |=   ^=   <<=   >>=Assignment operators
15,Comma operator

I think it's going to be easiest to prove these in reverse order, so let's take them one by one...

15 - Comma operator

Uniface doesn't have comma operators, you cannot put multiple lines of code on the same line with any separator, as far as I'm aware.

14 - Assignment operators

I wrote a previous post on assignment operators, the arithmetic ones (+= -= *= /= %=) all work in Uniface, but the logical ones (&= |= ^= <<= >>=) do not.  The simplest assignment operator (=) is also used as the comparison operator, there is no differentiation in Uniface.  This means that the comparison operator takes precedence, which be proved with a simple if statement...

x = 0
if ( x = 1 )
  ;if assignment took precedence
askmess "x=%%x%%%" ;shows "x=0"

13 - Conditional expression

This is not valid syntax in Uniface either, you have to do it the long way.  I never really liked this syntax in javascript though anyway, less readable in my opinion.

11/12 - Logical and/or

The logical operators in Uniface are singular (& and |), although the double versions (&& and ||) also work the same way, as I discovered in an earlier post.  I tried to combine these with the assignment operators in order to determine the precedence, but all I got was compile errors.  It seems that assignment operators cannot be used inline.

8/9/10 - Bitwise and/xor/or

Uniface does not have bitwise operators, as previously discussed.  The singular "and" and "or" operations are the logical operators.

6/7 - Comparison

As mentioned above, the equals comparison operation (=) is the same as the simplest assignment operator, and the comparison takes precedence.  It also takes precedence over the logical operators, as this if statement proves...

x = 0
if ( x = 1 | 1)
  x = 1 ;if ((x = 1) | 1)
  x = 0 ;if (x = (1 | 1))
askmess "x=%%x%%%" ;shows "x=1"

5 - Bitwise shift

Uniface still doesn't have bitwise operators :)

4 - Addition/subtraction

It's simple enough to show that the addition (+) and subtraction (-) operators take precedence over the comparison operator (=), using another if statement...

x = 0
if ( x = 0 + 1 )
  x = 1 ;if (x = (0 + 1))
  x = 0 ;if ((x = 0) + 1)
askmess "x=%%x%%%" ;shows "x=0"

3 - Multiplication/division/modulo

It's also simple to show that the multiplication (*), division (/) and modulo (%) operators take precedence over the addition and subtraction operators...

x = 6 * 1 - 1
;(6 * 1) - 1 = 5
;6 * (1 - 1) = 0
askmess "x=%%x%%%" ;shows "x=5"

2 - Unary/type cast

Uniface doesn't support all the unary operators, in fact I'm not sure what all of them do.  The not (!) operand is easy enough to test though...

x = ! 0 + 1
;(! 0) + 1 = 2
;! (0 + 1) = 0
askmess "x=%%x%%%" ;shows "x=0" but would have expected "x=2"

Surprisingly this one comes up with the opposite result than was expecting, indicating that addition (+) actually takes precedence over not (!).  We know that Uniface is good at type casting though, and will always try to cast a string as a numeric when doing arithmetic operations...

x = "1" * 2
askmess "x=%%x%%%" ;shows "x=2"

We can also show the negative unary operator (-) takes precedence over the subtraction operator (-), despite these being the same, like this...

x = -1 - 2
;(-1) - 2 = -3
;-(1 - 2) = 1
askmess "x=%%x%%%" ;shows "x=-3"

1 - Grouping

Uniface doesn't have arrays or members, and scope is handled explicitly with end statements.  It does however use brackets for grouping, as we've seen through these examples, they can be used to change the order of precedence, as they are always done first.

Summary: It seems that Uniface is a relatively simple in the sense that it doesn't do complicated inline nesting, nor any bitwise operations.  With the exception of the not operator (!), it seems to follow the standard order of operations though.  Personally I prefer to use lots of brackets in order to make the order of precedence more obvious and easier to read, without having to remember these rules.