Undefined (NOT), Optional and Default Variables

Undefined (NOT), Optional and Default Variables

Return to topic list

Wed May 25 04:46:15 2016   by   wyrdR
Hi Torben,

Any chance of implementing NOT operator (or undefined), optional and default variables in Troll?

Any attempt to reference a non-existent variable throws "Runtime error: unknown variable"

i.e.  A variable, whereby, if you DO NOT specify it on the command line, it will DEFAULT to a value in the definition.


x:=if !x then -1 else x;  \ If x not defined, is empty or doesn't exist, the x = -1, don't throw an error


x:=if defined x then x else -1;  \ If x not defined, the x = -1, don't throw an error


define x;  \ x is defined but has no value; It will be assigned later or from an argument



Wed May 25 09:58:49 2016   by   Torben
A negation operator would be fairly easy to implement, but in the mean time, you can just flip the branches of a conditional and use while instead of until (and vice-versa) in loops.

I'm not entirely happy about the idea of giving undeclared variables default values, as that will make some errors go unnoticed.  An operator that will return the value of x if this is defined but a specified value if not is a possibility, though.  I will think about it.
Fri Jun 3 09:08:26 2016   by   Torben
Out of curiosity, what would you want to use default values for?  It does not make sense for initialising a loop variable, as that is not how loops work (you can not reassign a new value to an existing variable).
Fri Jun 3 20:36:35 2016   by   Ron Vantreese
I came across this piece of information that might give you some ideas:


Near the bottom is a table describing how a nullable variable behaves in a logical test. Granted, it's about Boolean, but the same concept can be applied to other variables.

Returning a value if an object is null? Sometimes I get this value "(null)" when attempting to access a null object.
Sat Jun 4 08:19:36 2016   by   wyrdR
I want optional variables on the command line, making certain definitions more flexible.

Tue Jun 7 14:31:39 2016   by   Torben
I have now added negation and default values.

The negation operator is called ! (like in C). ! e is an abbreviation for if e then {} else 1.

The default value construction x ~ e checks if x is defined.  If it is, its value is returned. Otherwise, the value of e is returned.

If hope this solves your problem.
Wed Jun 8 02:20:56 2016   by   wyrdR
Thanks Torben,

That's awesomeness!
Wed Sep 7 23:04:55 2016   by   Jeoshua
Speaking of the ! operator, does anyone else find the "not equals" notation cumbersome? =/= may look like "does not equal" but it flies in the face of notation in most other programming languages. I would greatly prefer "!=" to mean "does not equal" as a filter.
Thu Sep 8 09:49:56 2016   by   Torben
=/= is used for "not equal to" in Erlang, Prolog, Fortress and possibly a few other languages.  So while the C crowd (including C-derived languages like Java and C#) may find it unfamiliar, its use is not unique to Troll.  Languages before C mostly used <>, .NE. or NE for inequality, but that is even farther from the mathematical symbol ≠. Some languages (such as Haskell and modern Fortran) use /=.

Return to topic list

New message:
Posted by:

Type the values of the dice shown below:

Return to topic list