Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Scripting Languages, Pros and Cons

Scripting Languages, Pros and Cons

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #15 on Thu 22 Feb 2007 11:17 PM (UTC)
Message
Shadowfyr, fire up your trusty text editor, enter the following:

int main()
{
  4+4;
  (4+4);
  return 0;
}


then compile it. Report back on what you get.

It is perfectly legal in very many language for an expression to be a statement. Now, the compiler will probably warn you about effectless statements (as above) but in general an expression is not necessarily without effects.

I'm curious what data points you have for making very broad statements about what "most compilers/interpreters" would do. For starters, every language behaves differently, so it's hard to make blanket statements about what "should" work in all language. And secondly, this construct is legal in many languages to begin with. So, I'm wondering what languages you are basing your claim on, because in many languages I know of, it is in fact perfectly legal to have expressions as statements without assignments etc.

Maybe QBasic doesn't like it, but then again, maybe QBasic isn't exactly a paradigmatic example of general programming languages.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #16 on Fri 23 Feb 2007 11:22 PM (UTC)
Message
Quote:

Its just damned odd, and most compilers/interpretters would freak when running across it, because they assume you completely missed some part of the line, such as an assignment. In fact, I would be willing to bet that most would return some error like, "Error on line blah. Missing = in assignment."


Do this in Jscript in MUSHclient:


(a = 1);

Note (a);


That compiles OK, and prints "1".

The same thing works in C or C++.

In those languages what look like statements, for example:


a = 1;


... are really expressions that happen to have side effects (in this case, changing the value of a).

In fact, you can also do this:


Note (a = 1);


In this case "a = 1" is setting the value of a to 1, and also returning 1 as the result of the expression, which is then printed. The point is, it is valid to leave the word Note out, it still sets a to 1, and the brackets are still valid.

It is valid to put expressions in brackets, so my example further back is perfectly OK.

Thus, the compiler does not object, as you predicted.

The nice thing about Lua is that it doesn't insist either on a statement terminator (like the ";" in C / C++ / Java), or a "there is one statement per line" mentality like VBscript.

But, in one, rather obscure case, you need to put the semicolon in, to avoid ambiguity.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shadowfyr   USA  (1,791 posts)  Bio
Date Reply #17 on Sat 24 Feb 2007 07:01 PM (UTC)
Message
Ok, ok. I guess most of them treat results returned by such a thing the same way they do if you where to define a function, but not assign the function to a result, i.e.:

function a(b)
a = b++
end function

a(5)

c = a(5)

Both of the calls are valid, one just does basically nothing. This is slightly odd, to me at least, in cases where the implication is that it "must" be passing a value. And yeah, I am most used to variations of Basic, as well as other languages, in which such a (a(5)) construct is not legal on the line by itself.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #18 on Sat 24 Feb 2007 08:09 PM (UTC)
Message
I think a problem is that you are assuming that a function does nothing but return a value, and has no side effects. If a function were indeed to never modify anything and only return values, then yes, it would be odd to call a function on its own.

But, a function can do all kinds of things:
- it can modify the objects passed in (if they're by reference)
- it can modify global values
- it can write to output (or something like that)

In all of those cases, and more, it makes sense to call a function and throw away its return value. Saying that a function call does nothing when you don't store its return value is a very dangerous statement to make. Even in VB that's not true (see e.g. my three scenarios above).

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Shaun Biggs   USA  (644 posts)  Bio
Date Reply #19 on Sat 24 Feb 2007 10:39 PM (UTC)
Message
Quite a lot of the time, functions have the thowaway values that are very useful for debugging or error catching. Like if you have a loadfile function that returns a 0 if everything is fine, a 1 if the file didn't exist, and a 2 if the file was corrupt, then you could display better error messages to the user, or to the program itself.

As for the 'example' that was given, a better one to show would be this:

function a(b)
  print d
  d = d++
  a = b++
end function

d = 1
a(5)
print d

c = a(5)
print d

Still think the first call of a(5) does nothing? For your example, it's not really demonstrating anything at all, since you could have just put the number 6 on the line by itself and had the same effect. I think the main problem is comparing BASIC to, well, pretty much anything useful. Most languages nowadays don't care about functions or procedures, and they treat them the same. Procedures just always return a value of nil.

It is much easier to fight for one's ideals than to live up to them.
Top

Posted by Shadowfyr   USA  (1,791 posts)  Bio
Date Reply #20 on Sun 25 Feb 2007 08:34 PM (UTC)
Message
Sigh. Not what I meant. I mean it does nothing with "any value returned". Sorry I wasn't clear about that. In a case like (4+4) by itself, the implication, to anyone that doesn't just like placing useless junk in their code, would be that "something" was supposed to happen to that value, not just throwing it out. Its like dumping NOP into the middle of machine language code, not because you need it for some critical timing issue (which with faster CPUs its almost worthless for), but "Just because I felt like it". The difference being, NOP is a valid instruction. Doing:

MOV A, &h4
PUSH A
ADD A, &h4
POP A

makes no damn sense. You're not doing anything. A good compiler/interpreter should "tell you" that its not going to do anything, or at least warn you, not just shrug its proverbial shoulders and go, "Well, ok. Not a problem." Something like the (a(5)) situation is a lot more ambigous, so... maybe you should be allowed to get by with it. But to me its still really odd syntax.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #21 on Sun 25 Feb 2007 08:49 PM (UTC)
Message
It is indeed odd to just put (4+4) on its own line. And the compiler I used (gcc 4) does warn you about it. However it's not illegal, and in general it would be rather hard to validate expressions in general as being with or without effects. That is why it's up to the programmer to deal with it.

As for (a(5)), that's a side effect of the syntax of expressions. Nick went over that, but basically it has to do with the fact that in many languages:
- expressions can be used as statements
- a(5) is an expression
- (expression) is an expression)
- therefore (a(5)) is an expression
- therefore (a(5)); on its own is an expression.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #22 on Mon 26 Feb 2007 05:15 AM (UTC)
Message
Quote:

... the implication, to anyone that doesn't just like placing useless junk in their code, would be that "something" was supposed to happen to that value ...

... A good compiler/interpreter should "tell you" that its not going to do anything, or at least warn you ...


True. And gcc does exactly that, if you compile with an appropriate warning level. The default doesn't.

This example demonstrates this:


int main (void)
  {
  5;
  return 0;
  } // end of main

$ gcc test.c -Wall
test.c: In function `main':
test.c:3: warning: statement with no effect


However your earlier position was:

Quote:

I would be willing to bet that most would return some error ...


The line with "5;" on it is an example of an expression that effectively does nothing. As you suggested, a warning can be issued. However, it does not raise an error, as it is within the boundaries of the syntax of the language.

Warnings are effectively saying "you are allowed to do this, but are you really sure it is a good idea?".



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


73,599 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.