PDA

View Full Version : Three questions - setting result at beginning, overloading commands, classes/objects



skollstagg
02-16-2016, 12:18 AM
1. Is it necessary to set the result of a function at the beginning? I remember seeing it done when I first started programming and have been doing it on autopilot ever since, but it really seems unnecessary in most situations.


function waitForX(x:Object):Boolean;
begin
Result := false;

while(not(find(x)))do {Just an example, I don't made infinite loops like this}
wait(250);

Result := true;
end;


2. Is it possible to overload certain commands such as break()? I'd like to have something like:


while(not(findX()))do
begin
if(time > 5000)then
break(y, -1, 'took too long');
end;


Instead of:


while(not(findX()))do
begin
if(time > 5000)then
begin
searchObj := y;
log(-1, 'took too long');
break();
end;
end;


3. Is it possible to create objects/classes? I saw a thread where someone did do it, but I was unable to do it myself.

tls
02-16-2016, 12:26 AM
1. No, but it feels safer.
2. Try it?
3. Not true OOP Objects, no. You can make records that have namespaced functions but it's not the same thing.

skollstagg
02-16-2016, 12:30 AM
1. No, but it feels safer.
2. Try it?
3. Not true OOP Objects, no. You can make records that have namespaced functions but it's not the same thing.

I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask

Currently doing record/namespace. Shame we can't do it

Thanks!

honeyhoney
02-17-2016, 11:37 AM
I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask

I don't think you can overload a keyword (despite it being implemented as a procedure).

the bank
02-17-2016, 12:56 PM
I did try to overload break, however it didn't work. Couldn't find anything on google. Pretty sure it can't be done, but I thought I'd ask

Currently doing record/namespace. Shame we can't do it

Thanks!

Yes, it is a shame. The lack of inheritance, polymorphism, and all around OOP awesomeness actually makes me hate Simba on levels you don't even understand sometimes.

You can't overload the break keyword. At most, you could make an alias for it, and I don't know if that is even allowed in Lape. You could however write your own function that will do what you want as well as break...

Lastly, please keep in mind the exit() keyword. When used within a function, it acts as a return, and is much nicer on the eyes than "result". ie. exit(true); exit(false); exit(42); etc

skollstagg
02-17-2016, 08:09 PM
Yes, it is a shame. The lack of inheritance, polymorphism, and all around OOP awesomeness actually makes me hate Simba on levels you don't even understand sometimes.

You can't overload the break keyword. At most, you could make an alias for it, and I don't know if that is even allowed in Lape. You could however write your own function that will do what you want as well as break...

Lastly, please keep in mind the exit() keyword. When used within a function, it acts as a return, and is much nicer on the eyes than "result". ie. exit(true); exit(false); exit(42); etc

Normally that wouldn't be an infinite loop and I'd have exits within, but would it be good practice to always use exit regardless?

Could you also tell me how I can create a function that will act as a break? I couldn't think of a way before and didn't really know what to google..

slacky
02-17-2016, 08:42 PM
but would it be good practice to always use exit regardless?
Exit is nice when, and really only when it enhances the readability of the code, eg a lot functions do checks at the "start" which will return if the arguments doesn't match some criteria. This is when Exit should be used.
Using Exit in the middle (of a larger function) can in cases degrade the readability.
Using Exit at the end of a function just to use it would be pointless. And avoiding using the "Result" variable would be rather pointless as well, since it's already there, and you plan on returning something anyway.

In other words, usage of "Exit" depends, there are no clear cut answers, other than to use your head.


function sum(x:Array of Int32): Int64;
var i:Int32;
begin
for i:=0 to High(x) do
Result += x[i];
end;

Here you see that the Exit keyword simply isn't needed, and the result-variable does the trick. It would be bad practice to use Exit here as it's simply not needed..


Could you also tell me how I can create a function that will act as a break? I couldn't think of a way before and didn't really know what to google..
This is not doable. Break will break out of a loop, you can have a procedure or a function do this. Break is builtin to the language simply because this functionality can't be created/duplicated on the scriptside.

What you can do is:

while(not(FindX()))do
begin
if (time > 5000) then
begin
DoStuff(y,-1,'took too long');
Break;
end;
end;

Where DoStuff does whatever you need to do with "y" and also calls the "log"-method. Just like you wanted to do by overloading "break".

Also modifying break would only make your code less readable, shorter isn't always better. This is a clear case where it's NOT. Same goes for the example I wrote above, it's less readable then the original code.


As for your first post, setting result at the beginning isn't needed unless default return value should not be the same as the value lape inits your variable to.
Booleans default to False, so for your example it isn't needed in lape.

For other languages, including Delphi and FPC you need to initialize every variable before you use em, else you would end up with unexpected outcome, as these languages do not initialize your variables to some default.

the bank
02-17-2016, 09:22 PM
In other words, usage of "Exit" depends, there are no clear cut answers, other than to use your head.


function sum(x:Array of Int32): Int64;
var i:Int32;
begin
for i:=0 to High(x) do
Result += x[i];
end;

Here you see that the Exit keyword simply isn't needed, and the result-variable does the trick. It would be bad practice to use Exit here as it's simply not needed..


Correct me if I'm wrong, but in your example exit wouldn't only be unneeded but infact wouldn't allow the function to work properly at all. Exit would act as a return and exit the function during the first iteration. Assuming my understanding is correct.

slacky
02-17-2016, 09:31 PM
Correct me if I'm wrong, but in your example exit wouldn't only be unneeded but infact wouldn't allow the function to work properly at all. Exit would act as a return and exit the function during the first iteration. Assuming my understanding is correct.
The only way to use exit in that code would be to use it like "return" in other languages, which would be pointless since we have that lovely "Result" variable:


function sum(x:Array of Int32): Int64;
var i:Int32;
begin
for i:=0 to High(x) do
Result += x[i];
Exit; //or Exit(Result);
end;

Point was that the above Exit is totally pointless in Pascal.

Even if you have tiny function like:

function squared(x:Int32): Int32;
begin
Result := x*x;
end;

You should not replace Result with Exit here, there is no reason to use Exit, it's simply not ment for this purpose, Exit is and should be used when you need to return earlier then the functions builtin return-point.

There is in fact a person (maybe a few) on this forum that does stuff like that: "Exit(Result)" and "Exit(x*x)" (for absolutely no reason at the end of a function), a long with a bunch of other questionable stuff. like using pointers everywhere... and it's really not a pretty view.
hint: oglib

skollstagg
02-17-2016, 10:09 PM
Excellent that's how I currently use exit/result. Been trying to stop myself setting result at the beginning bit it's like a habit at this point


function TInterface.openWindow(window:TWindow):Boolean;
var
t:Longword;
begin
if(window.isOpen())then
exit(true);

markTime(t);
while(true)do
begin
if(timeFromMark(t) > 40000)then
logTerminate('TInterface.openWindow: Fail: ' + window.s);

if(window.openKey > -1)then
typeKey(window.openKey)
else
mouseBox(window.b, 1, 7);

if(waitFor(@window.isOpen, 5000))then
exit(true);
end;
end;



function TWindow.isOpen():Boolean;
begin
result := findObj(self.obj);
end