Syntax questions: one-liner try catch block, multiple statement in one line

This forum is meant for questions and discussions about the X# language and tools
Post Reply
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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#

Code: Select all

VAR a := 0
a += 1, a += 2
How about (would be nice):

Code: Select all

VAR a := 0, a += 1, a += 2
*****
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
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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#
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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
});
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Syntax questions: one-liner try catch block, multiple statement in one line

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply