Page 1 of 1
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Thu May 24, 2018 10:03 am
by SHirsch
Hi,
in C# I use 'try catch' block in one single line:
Code: Select all
try { ... } catch(Exception ex) { ... }
Is this possible in X#? Currently 5 line are used.
*****
next question:
this is possible code in X#
How about (would be nice):
*****
another possible one-liner in c#:
Code: Select all
foreach(var p in ps) /* do something*/;
Currently in X# 3 lines are use.
*****
Sometimes one-liners are more readable.
Regards,
Stefan
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Thu May 24, 2018 11:10 am
by FFF
Stefan Hirsch wrote:Sometimes one-liners are more readable.
Yes, sometimes. IMHO rarely
BTW,a try shows, that
FUNCTION Start() AS VOID
VAR a :=1; a:=2
? a
RETURN
works.
I personally wouldn'd do this, as with tired eyes & mind it calls for oversight...
HTH
Karl
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Thu May 24, 2018 11:30 am
by robert
Stefan,
You should be able to put the commands on one line if you use the semi colon delimiter:
Code: Select all
FUNCTION start AS VOID
LOCAL nVar AS LONG
nVar := 0
TRY ; nVar := nVar / nVar ; CATCH e AS Exception ; ? e:Message ; END TRY
Console.Read()
RETURN
I see that that does not work in the current compiler. The CATCH clause seems to not like it when it is not at the start of the line.
This does work:
Code: Select all
TRY ; nVar := nVar / nVar
CATCH e AS Exception ; ? e:Message ; END TRY
Robert
But I really don't understand why you would want to transform X# into a C# clone. In that case I would just use C#
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Thu May 24, 2018 12:21 pm
by SHirsch
Hi Robert,
Thanks for info.
I do not want to transform X# into C#. I don't like all the brackets. I try to get the best from all
I think the more flexible a language is the more people may use it.
Regards,
Stefan
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Fri May 25, 2018 8:47 am
by MathiasHakansson
Would it be possible to do something similar to this in X#? It could save som lines, or it could be easier to make a one liner with...
public static void TryCatch(Action action)
{
try
{
action();
}
catch (Exception ex)
{
BCNMessageBox.ShowException(ex);
}
}
Use it like this:
BCNApplication.TryCatch(() =>
{
// Do something
});
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Fri May 25, 2018 9:34 am
by robert
Mathias,
Yes. Try this (X# core dialect)
Code: Select all
FUNCTION Start AS VOID
LOCAL nVal AS LONG
TryCatch ( { => nVal := nVal / nVal })
? nVal
Console.ReadLine()
RETURN
FUNCTION TryCatch(action AS Action) AS VOID
TRY
action()
CATCH e AS Exception
? e:Message
END TRY
RETURN
If you want to execute statements you will have to specify a multi line lambda expression like this:
Code: Select all
FUNCTION Start AS VOID
LOCAL nVal AS LONG
TryCatch ( { =>
? nVal := nVal / nVal
})
Console.ReadLine()
RETURN
Robert
Syntax questions: one-liner try catch block, multiple statement in one line
Posted: Fri May 25, 2018 9:41 am
by robert
Mathias,
And to make it even more "sexy", you can also use the preprocessor to hide the lambda syntax:
Code: Select all
#xcommand MYTRYCATCH <*any*> => TryCatch ( { => <any> }
FUNCTION Start AS VOID
LOCAL nVal AS LONG
MYTRYCATCH nVal := nVal / nVal
Console.ReadLine()
RETURN
FUNCTION TryCatch(action AS Action) AS VOID
TRY
action()
CATCH e AS Exception
? e:Message
END TRY
RETURN
Robert