Jump to content

Variable prefix/suffix characters?


Fenix Eldritch
 Share

You are about to reply to a thread that has been inactive for 480 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Continuing a conversation here so as to not detail another thread.

In the referenced thread, a side question was asked if it was allowed to have variable names that start with the "$" character. I did a quick test and found that yes, that's allowed, along with several other characters:

default
{
    state_entry()
    {
        string      `a = "'";
        llOwnerSay( `a);
        //string      !b = "!";
        //llOwnerSay( !b);
        //string      @c = "@";
        //llOwnerSay( @c);
        string      #d = "#";
        llOwnerSay( #d);
        string      $e = "$";
        llOwnerSay( $e);
        //string      %f = "%";
        //llOwnerSay( %f);
        //string      ^g = "^";
        //llOwnerSay( ^g);
        //string      &h = "&";
        //llOwnerSay( &h);
        //string      *i = "*";
        //llOwnerSay( *i);
        string      éj = "é";
        llOwnerSay( éj);
        //string      -k = "-";
        //llOwnerSay( -k);
        //string      _l = "_"; //this is valid
        //llOwnerSay( _l);
        //string      =m = "=";
        //llOwnerSay( =m);
        //string      +n = "+";
        //llOwnerSay( +n);
        //string      [o = "[";
        //llOwnerSay( [o);
        //string      {p = "{";
        //llOwnerSay( {p);
        //string      |q = "|";
        //llOwnerSay( |q);
        string      \r = "\\";
        llOwnerSay( \r);
        string      's = "'";
        llOwnerSay( 's);
        //string      ;t = ";";
        //llOwnerSay( ;t);
        //string      :u = ":";
        //llOwnerSay( :u);
        string      ?v = "?";
        llOwnerSay( ?v);
        //string      <w = "<";
        //llOwnerSay( <w);
        //string      ,x = ",";
        //llOwnerSay( ,x);
        //string      .y = ".";
        //llOwnerSay( .y);
    }
}

//outputs:	' # $ é \ ' ?

So... I thought that they were just being accepted as characters in the name. Qie pointed out that variable names can also be suffixed by those characters. And I initially assumed, "yeah obviously, they'll work anywhere in a var name" ... but upon testing that assumption, I find I was wrong.

string ?x  = "?"; //works
string ?x? = "?"  //works
string x?x = "?"  //does not compile

And what's more, I just noticed that I can reference the variable name sans special character.

string     ?v = "?";
llOwnerSay( v);       //compiles ok

So those aren't even contributing to the var name. Can someone explain what's happening here? And what those characters mean in other programming contexts? The significance is lost on me at the moment.

Edit: and this on the official Linden viewer
Edit: whoops "_" is a valid and recognized character for var names

Edited by Fenix Eldritch
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • Fenix Eldritch changed the title to Variable prefix/suffix characters?

There are several surprises in that. Probably the most confusing one is your last discovery, that you can reference the variable name without the special character. That makes variables like ?v a curiosity, but not a very appealing one.  Too many chances for ambiguity, especially for those of us who are typo challenged.

  • Like 2
Link to comment
Share on other sites

53 minutes ago, Fenix Eldritch said:

And what those characters mean in other programming contexts?

As 2Tessa mentions, the "#" symbol is a common prefix for preprocessor directives (including the C preprocessor, where I first encountered it). But that's all external to the LSL language itself, and its use might be considered a bug in the LSL preprocessor as long as it behaves as it does in LSL.

The "$" is a common type sigil, notably in BASIC.

A "?" is a common ternary operator separating the condition from the "true" expression (with ":" separating the "else").

But it kinda feels like these symbols are acting as whitespace when affixed to a token—and not only variable names; "#llOwnerSay" works just fine.

________________

ETA, this is interesting from the sigil wikipedia entry:

Quote

In Common Lisp, special variables (with dynamic scope) are typically surrounded with * in what is dubbed the "earmuff convention". While this is only convention, and not enforced, the language itself adopts the practice (e.g., *standard-output*). Similarly, some programmers surround constants with +.

(I don't remember any of this from my Common Lisp days, but that doesn't mean anything.)

Edited by Qie Niangao
  • Like 4
Link to comment
Share on other sites

27 minutes ago, Qie Niangao said:

As 2Tessa mentions, the "#" symbol is a common prefix for preprocessor directives (including the C preprocessor, where I first encountered it). But that's all external to the LSL language itself, and its use might be considered a bug in the LSL preprocessor as long as it behaves as it does in LSL.

The "$" is a common type sigil, notably in BASIC.

A "?" is a common ternary operator separating the condition from the "true" expression (with ":" separating the "else").

I find it interesting how each of these is used differently in different languages.

# -  In C, etc.: Preprocessor;  I could have sworn it was a type specifier in some BASIC, but I was probably thinking of "%" for "integers".

$ - In Old Assembly: Hex specifier; In BASIC, "string".

? - In C, etc.: Ternary; In BASIC, "print".

 

  • Like 1
Link to comment
Share on other sites

Since the OP of that previous thread seems to be coming from a PHP background, I'll point out a couple things:

$ is used as a required part of PHP's syntax for variables, I imagine that's why OP in the previous thread has been using it.

The question mark symbol is also used in various modern languages as a null-safe operator, or as a null-coalescing operator.

As in:

$object?->variable; // Only accesses 'variable' in '$object' if the object exists
$data ?? "some value"; // Returns the value from '$data' if it exists, otherwise returns 'some value'

 

The other characters (besides $ and ?) seem to be treated as whitespace in LSL as well.

Edited by Wulfie Reanimator
  • Thanks 1
Link to comment
Share on other sites

On 1/6/2023 at 11:50 PM, Fenix Eldritch said:

So... I thought that they were just being accepted as characters in the name. Qie pointed out that variable names can also be suffixed by those characters. And I initially assumed, "yeah obviously, they'll work anywhere in a var name" ... but upon testing that assumption, I find I was wrong.

string ?x  = "?"; //works
string ?x? = "?"  //works
string x?x = "?"  //does not compile

And what's more, I just noticed that I can reference the variable name sans special character.

string     ?v = "?";
llOwnerSay( v);       //compiles ok

So those aren't even contributing to the var name. Can someone explain what's happening here? And what those characters mean in other programming contexts? The significance is lost on me at the moment.

Does this apply to all non-letters symbols, or just some?

Like, for instance, the "$" prefix/suffix. Will it be silently discarded as well?

Link to comment
Share on other sites

1 hour ago, primerib1 said:

Does this apply to all non-letters symbols, or just some?

Like, for instance, the "$" prefix/suffix. Will it be silently discarded as well?

"$", "?", and "\" will be discarded, but some symbols like "." or "_" will not be.

(The underscore is a valid symbol for variable names, but the period isn't and will cause syntax errors.)

Edited by Wulfie Reanimator
  • Thanks 1
Link to comment
Share on other sites

On 1/7/2023 at 12:44 AM, Quistess Alpha said:
default?
{
    ?state_entry?(?)
    {
        string? test? = ?"string";
        ?llOwnerSay(test);
    }
    ?touch_start?(?integer?n)
    {   ?while?(~?--?n)
        {   llRegionSayTo(llDetectedKey(n),0,"Touched");
        }
    }
}

as evidence for the whitespace hypothesis.

I noticed you set "test?" but then say "test".

So the question mark is stripped out and ignored.

Interesting.

That means if I mark a variable, say "locked?", I can't create another variable say "locked$"

 

Link to comment
Share on other sites

1 hour ago, primerib1 said:

I noticed you set "test?" but then say "test".

So the question mark is stripped out and ignored.

The characters Fenix identified (` # $ é \ ' ?) are not exactly ignored, I think, but rather treated as whitespace. That would explain the syntax error when used "infix" : it results in two separate tokens. And the working syntax here:

default
{
    state_entry()
    {
        state#foo;  // The "#" as whitespace
    }
}

state foo
{
    state_entry()
    {
        llOwnerSay("Foo!");
    }
}

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 480 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...