Using something similar to \n rather than empty WriteLn() in free pascal - freepascal

procedure A();
//Do something useful
WriteLn('This is a message');
procedure B();
//Do something else useful
WriteLn(); //<---Empty WriteLn();
WriteLn('This is another message');
procedure C();
//Do something else useful
A();
B();
WriteLn(); //<---Empty WriteLn();
WriteLn('This is another message from C');
This is only 3 procedures, and my program has more than 3 procedures like this...Is there a way that I don't have to put empty WriteLn(); (something like \n in html) in most procedures because of aesthetic reasons?
Thanks!

The proper way to do this is actually to use an empty Writeln. A second best, also platform-independent way is to use something like:
Writeln(sLineBreak + sLineBreak + 'This is another message');
(sLineBreak is defined in unit System, AFAIK).
But if you already think that using an empty Writeln is too much, then you can insert the carriage return and line feed characters in the string, as below:
Windows
Just add a carriage return (#13) /line feed (#10) combination:
procedure B();
begin
//Do something else useful
WriteLn(#13#10'This is another message');
end;
You can even have multiple empty lines like that:
Writeln(#13#10#13#10'Hello');
Writeln('-----'#13#10#13#10);
Non-Windows
If you are on a non-Windows platform, probably a #10 (line feed) will do. Most non-Windows platforms do not need the carriage return.
FWIW, instead of #10 you can also use ^J and instead of #13 you can use ^M. These are equivalents:
Writeln(^M^J^M^J'Hello');
Writeln('-----'^M^J^M^J);
^M is Ctrl+M, which has ASCII code 77 - 64 = 13! ^J is 74 - 64 = 10.

Related

Julia Try Catch statement not catching all errors

I've written a function which contains a list of sub functions to evaluate. I have a try-catch statement around this block of sub functions so that if one of them throws an error the catch statement will ensure that the variables the subfunctions return will just be set to text messages.
Example Subfunction (working as expected)
function f(x)
SQRT = sqrt(x)
return SQRT
end
f(9) #returns the square root of a positive number
> 3.0
f(-9) #error when you try to take the square root of a negative number
> LoadError: DomainError: while loading In[80], in expression
starting on line 1 in f at In[76]:2
Example Main Function with try-catch statement (not working as expected)
function g(y)
a=[]
b=[]
try
a=f(y)
b="works"
catch
a="no"
b="no"
end
return a,b
end
g(9)
> (3.0,"works")
g(-9)
> LoadError: DomainError: while loading In[96], in expression
starting on line 1 in f at In[76]:2
I understand that the error is correct when I use f(x) since I haven't used a try-catch statement. However I'm expecting that the try-catch statement would catch this error when f(y) is evaluated inside of g(y) and would return a = "no" and b = "no". Why is the try-catch statement not picking this error up and working as expected?
Is there a way to get this statement to work? I need it to work this way to avoid putting try-catch statements into all of the many subfunctions I have written. In this example it would be easy to just insert the try-catch into f(x) and the problem would be solved as per below, but I don't want to do this in my real code because the subfunctions could fail in a number of places and I'd have to significantly alter my code to take account of all of the potential failures.
Example Subfunction with try-catch statement (working as expected)
function h(x)
SQRT = []
try
SQRT = sqrt(x)
catch
SQRT = "no"
end
return SQRT
end
h(9)
> 3.0
h(-9)
> "no"
Example Main Function (working as expected)
function i(y)
a=h(y)
b="works"
return a,b
end
i(9)
> (3.0,"works")
i(-9)
> ("no","works")
In other words, I don't want to have to use the method in my second example, is there a way to get what I want using something similar to my first example?
I'm using Julia version 0.4.6
Thanks StefanKarpinski for suggesting a restart of the repl session, that worked.
"I've tried this on Julia 0.3, 0.4 and 0.5-dev and it works in all of them. Please do try in a clean session and see if you can reproduce. Otherwise, please give details of what version of Julia you're using.
– StefanKarpinski Jul 6 at 4:55"

What's the best way to make a parameter of a function mandatory?

In Coffeescript, what's the best way to create a function with mandatory parameters? At the moment, I'm doing this:
myFunction: (requiredParam, optionalParam) ->
unless requiredParam? then throw new Error ...
...
If I have, say, 5 parameters or even more, it takes quite a lot of time and space to throw an error for each parameter.
Is there a simpler/more concise way of doing this?
You can use the arguments variable inside any function to ensure that the number of actual arguments is not smaller than the number of required arguments.
myFunction: (required1, required2, required3, optional1, optional2) ->
throw new Error("Given #{arguments.length} out of 3 required.") if arguments.length < 3
# ...

Tcl procedure return vs. set as return operator

Let us say that I have a procedure, and I wish to return a value of a variable local to the procedure.
There are many ways to do that, but 2 very simple ones, are, at the very last line of the procedure puts either of these command (assume that ret stores the returned value)
set ret ; #first option
return $ret ; #second option
What is the difference (if any) under the hood, between the 2 in Tcl, that should prompt me to choose 1 or the other?
Thanks
The bytecode sequence is effectively the same; both push the value onto the operand stack and call the done operation which terminates the procedure successfully. I usually prefer to use return as that's clearer as to what I mean.
I think it's more idiomatic in Tcl to use return. Unlike, for example, Ruby where it is idiomatic to avoid using return.
Also, if you're returning a non-ok status, you really have to use return, so you might as well use it for ok statuses too.

Lazarus (Free Pascal). Seemingly unnecessary compiler warning

I'm new to Free Pascal and Lazarus. I'm testing out the new "advanced records" feature and getting what looks to be an unnecessary compiler warning.
The code is very simple, just a record with a single method to initialize the data fields. Something like:
{$mode delphi}
type TTest =
record
a,b: double;
procedure Init(x,y: double);
end
procedure TTest.Init(x,y: double);
begin
test.a := x;
test.b := y;
end;
var t: TTest;
begin
t.Init(0.1,0.2);
That last line shown, "t.Init()", always generates a compiler warning that 'variable "t" does not seem to have been initialized'.
Ok it's only warning, so obviously I can live with it, but I just want to make sure it's not due to some underlying misunderstanding on my part.
EDIT to add new information.
Re the Method verus Constructor point.
Looking into this some more I'm kind of glad that FPC doesn't allow constructors for records. Delphi has constructors but no destructors, and as far as I can tell constructors (for records) do absolutely nothing that a method couldn't do. I'm a minimalist, so to me there is no point to have extra syntax that does nothing more than some existing syntax does.
Particularly for a simple record like this, the very act of declaring it (var t: TTest) does all the "constructing" that this record needs. Anything else (like my Init) is just setting values, it's not constructing anything anyway. So as far as I can see constructors are not the answer, the compiler just needs to drop the warning.
Researching the issue I have come across one other technique that is often used, the so called "factory function". That is, a function outside of the record such as:
function InitTTest(x,y: double): TTest;
begin
Result.a := x;
Result.b := y;
end;
This technique does indeed work, and gets rid of the warning message. I don't like this method however and I wont use it. Advanced records don't have inheritance and other features of classes, encapsulation is all they've got going for them. So I really dislike having to make a "factory function" outside of the record like this.
Please bear in mind the the above is just the opinion of a "noob", after doing just a little research to answer my own question here. So if I'm wrong about anything above I'm quite happy to be corrected. :)
Yes, that is what constructors are for, the following compiles with FPC trunk and produces no warnings:
{$mode delphi}
type TTest =
record
a,b: double;
constructor Init(x,y: double);
end;
constructor TTest.Init(x,y: double);
begin
a := x;
b := y;
end;
var t: TTest;
begin
t.Init(0.1,0.2);
end.

How can I throw an exception in Matlab?

I am writing some code and for now I am making some functions, but I'm not writing them yet. I'm just making an empty function that will do nothing yet. What I would like to do is throw an exception if the function is run, to prevent me from forgetting writing the function.
The easiest way is:
error('Some useful error message.')
Matlab is happier is you assign an identifer to you error message, like this:
error('toolsetname:other_identifying_information','Some useful error message here.')
The identifying information is reported with some of the error handling routines, for example, try running lasterror after each of the above calls.
You can also use:
throw(MException('Id:id','message'));
There is a nice feature to MException, it can be used as sprintf:
throw(MException('Foo:FatalError',...
'First argument of Foo is %s, but it must be double',class(varargin{1}) ));
As commented correctly by #edric, this sprintf functionality can be a double edged sword. If you use some of the escape characters, it might behave not like you want it.
throw(MException('Foo:FatalError',...
'I just want to add a \t, no tab!' ));
Did you read the MATLAB documentation for "Throwing an exception"?