LTSL: Return Values

The S&M crowd is getting naughty with the game files.

LTSL: Return Values

Postby Flatfingers » Sun Jul 20, 2014 2:43 pm

A post from thesilverspanch just reminded me that I wanted to ask about return values: are they call-by-reference?

That is, if function_X calls function_Y, and passes parameter Z to function_Y, can I assign a new value to Z within the scope of function_X?

If so, is that new value for Z returned to function_X? Even if Z is a complex variable such as an array?

Alternately, must all information returned to a calling function be passed through a single return value?
User avatar
Flatfingers
Vice Admiral
 
Posts: 4545
Joined: Sat Nov 24, 2012 12:45 am

Re: LTSL: Return Values

Postby Katawa » Sun Jul 20, 2014 4:21 pm

inb4 stateless functional programming discussion occurs
woops, my bad, everything & anything actually means specific and conformed
User avatar
Katawa
Rear Admiral
 
Posts: 1544
Joined: Thu Apr 18, 2013 1:05 am
Location: that sound that makes the hairs on your neck raise

Re: LTSL: Return Values

Postby Flatfingers » Sun Jul 20, 2014 9:44 pm

Katawa wrote:inb4 stateless functional programming discussion occurs

Heh. Sans doute.

I figured I'd ask, anyway.
User avatar
Flatfingers
Vice Admiral
 
Posts: 4545
Joined: Sat Nov 24, 2012 12:45 am

Re: LTSL: Return Values

Postby Soranya » Thu Jul 31, 2014 11:52 am

This reminds me how much i like Languages that are close to the actual Architecture/Hardware/System :P

Also i do not understand the Answer sadly, can someone explain?
Soranya
Ensign
 
Posts: 18
Joined: Mon Jun 09, 2014 3:39 am

Re: LTSL: Return Values

Postby Cornflakes_91 » Thu Jul 31, 2014 12:08 pm

I dont see a conflict between stateless functions and reference transfered variables? O.o
User avatar
Cornflakes_91
Admiral
 
Posts: 9198
Joined: Wed Mar 06, 2013 1:53 am
Location: Austria

Re: LTSL: Return Values

Postby Flatfingers » Thu Jul 31, 2014 2:13 pm

I'm still curious about this as well.

I had been thinking that an expression like (++ i), such as you might find in a for-loop, meant that the value of i was being changed in the function for which ++ was defined as an alias.

But it's since dawned on me that the result of (++ i) could just be the return value. i doesn't have to change.

So now I don't know.
User avatar
Flatfingers
Vice Admiral
 
Posts: 4545
Joined: Sat Nov 24, 2012 12:45 am

Re: LTSL: Return Values

Postby Cornflakes_91 » Thu Jul 31, 2014 2:17 pm

I guess i would have to be incremented to make ++ work as it is used to work.

I personally think that ++ works the classic way, albeit only in pre-increment kode
User avatar
Cornflakes_91
Admiral
 
Posts: 9198
Joined: Wed Mar 06, 2013 1:53 am
Location: Austria

Re: LTSL: Return Values

Postby thesilverspanch » Thu Jul 31, 2014 2:22 pm

Im not sure (++ i) is anything like ++i from c++. As far as I can tell, the syntax seams to be (operator var1 var2 ... varN) where the operator is applied to all the vars. There isnt anything so far in Joshs code that we've seen to say if (++ i) is pre or post increment. The ++ at the beginning is just the operator that is being applied to the rest of the variables in the expression, so you could do (++ i j k) and it would increment all of them. Im kind of assuming that its a post-increment, but there is no way to tell as of yet.
User avatar
thesilverspanch
Lieutenant Commander
 
Posts: 126
Joined: Sun Jul 06, 2014 9:18 pm
Location: Chico California

Re: LTSL: Return Values

Postby JoshParnell » Thu Jul 31, 2014 5:54 pm

Parameters are passed by (non-const) reference. This makes LTSL powerful, dangerous, and simple all at the same time :D

Over the years I have gone back and forth many times on constness. Although I adhere to good-practice usage of const in C++, in my present state, I find myself largely unimpressed with the utility that it provides. Differentiating constness for purposes other than optimization has proven of little value to me over the course of writing the LT engine. Contrast that with static typing, which has proven itself worthwhile on far-too-numerous occasions. Now, when we start talking about compiler optimization, constness is a huge deal, and I have big plans for LTSL and constant analysis. But IMO that is an entirely different issue from whether the notion of actually annotating constness is useful to the user. My belief is that analysis of constness is crucial, while annotation of constness is largely unnecessary (unless we are talking about applications wherein having write-protected memory is essential to the very security of the process. This is obviously not the case with LTSL).

So the end result is that yes, functions can modify the values of their parameters in LTSL. If that value happens to be an L-value (in other words, it was passed via a variable or member of a variable), then the value will actually be modified. If the value is not an L-value, the assigment will have no lasting effect outside of the function (contrast this to c++, which (for reasons that are either arbitrary or not understood by myself) restricts pass-by-nonconst-reference to L-values).
“Whether you think you can, or you think you can't--you're right.” ~ Henry Ford
User avatar
JoshParnell
Developer
 
Posts: 4261
Joined: Sun Oct 07, 2012 3:06 pm
Location: Baton Rouge, LA

Re: LTSL: Return Values

Postby Cornflakes_91 » Thu Jul 31, 2014 10:03 pm

thesilverspanch wrote:Im not sure (++ i) is anything like ++i from c++. As far as I can tell, the syntax seams to be (operator var1 var2 ... varN) where the operator is applied to all the vars. There isnt anything so far in Joshs code that we've seen to say if (++ i) is pre or post increment. The ++ at the beginning is just the operator that is being applied to the rest of the variables in the expression, so you could do (++ i j k) and it would increment all of them. Im kind of assuming that its a post-increment, but there is no way to tell as of yet.


from what i know is that expressions are evaluated from the inside out, so when you have

Func( ++ i) it first increments i (executing the innermost function) and then evaluates the surrounding function.

So ++ i is pre-increment out of execution order
User avatar
Cornflakes_91
Admiral
 
Posts: 9198
Joined: Wed Mar 06, 2013 1:53 am
Location: Austria

Re: LTSL: Return Values

Postby Katawa » Thu Jul 31, 2014 10:16 pm

Why was angelscript insufficient for lt?
woops, my bad, everything & anything actually means specific and conformed
User avatar
Katawa
Rear Admiral
 
Posts: 1544
Joined: Thu Apr 18, 2013 1:05 am
Location: that sound that makes the hairs on your neck raise

Re: LTSL: Return Values

Postby JoshParnell » Thu Jul 31, 2014 10:56 pm

The syntax is (function arg1 arg2 arg3 ...). So (++ i) means ++(i) and ++ is aliased to Int_Increment. So internally we are calling Int_Increment(i).

Since I don't find the return value of assignment to be a constructive endeavor, Int_Increment is void returning. So (MyFunction (++ i)) has no meaning, because (++ i) has no return type. OTOH (MyFunction (+ i 1)) is well-define, of course :)

Katawa wrote:Why was angelscript insufficient for lt?


It wasn't. But I didn't really want to script in c++ :) In the early days of the LT engine it was the first scripting engine that I integrated. At one point I had it working in the engine. But I decided that c++-based scripts were not how I wanted to go!
“Whether you think you can, or you think you can't--you're right.” ~ Henry Ford
User avatar
JoshParnell
Developer
 
Posts: 4261
Joined: Sun Oct 07, 2012 3:06 pm
Location: Baton Rouge, LA

Re: LTSL: Return Values

Postby thesilverspanch » Thu Jul 31, 2014 11:33 pm

Cornflakes_91 wrote:from what i know is that expressions are evaluated from the inside out, so when you have

Func( ++ i) it first increments i (executing the innermost function) and then evaluates the surrounding function.

So ++ i is pre-increment out of execution order


Ah! Good point! Sometimes I forget that with recursive expression calling, everything is inside-out and backwards!
User avatar
thesilverspanch
Lieutenant Commander
 
Posts: 126
Joined: Sun Jul 06, 2014 9:18 pm
Location: Chico California

Re: LTSL: Return Values

Postby Katawa » Thu Jul 31, 2014 11:57 pm

thesilverspanch wrote:Ah! Good point! Sometimes I forget that with recursive expression calling, everything is inside-out and backwards!

Related:
http://en.wikipedia.org/wiki/Currying
http://en.wikipedia.org/wiki/Partial_application
woops, my bad, everything & anything actually means specific and conformed
User avatar
Katawa
Rear Admiral
 
Posts: 1544
Joined: Thu Apr 18, 2013 1:05 am
Location: that sound that makes the hairs on your neck raise

Re: LTSL: Return Values

Postby boxofrox » Sun Jan 11, 2015 10:54 pm

Also related:
http://en.wikipedia.org/wiki/Lazy_evaluation

The few lisp dialects (Common LISP, Scheme) I've looked at use lazy evaluation, so I imagine LTSL uses it, too.

In the case of
Code: Select all
for i 0 (< i 5) (++ i)
    ...
    do stuff
    ...
the condition and increment functions aren't evaluated immediately, but as needed by the for function. I never did grasp how the LISP interpreter knew how to recognize which functions should be lazy and which shouldn't. At least, not without creating a special case in the interpreter when reading a for block, but that seemed contrary to the simplicity of LISP's syntax.
boxofrox
Recruit
 
Posts: 2
Joined: Sun Jan 11, 2015 9:25 pm


Return to Scripting & Modding



Who is online

Users browsing this forum: No registered users and 2 guests

cron