fbpx

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

More
7 months 3 weeks ago #1 by Stefan Hirsch
Hi,

in C# I use 'try catch' block in one single line:
try { ... } catch(Exception ex) { ... }

Is this possible in X#? Currently 5 line are used.

*****
next question:
this is possible code in X#
VAR a := 0
a += 1, a += 2

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

*****
another possible one-liner in c#:
foreach(var p in ps) /* do something*/;
Currently in X# 3 lines are use.
*****
Sometimes one-liners are more readable.

Regards,
Stefan

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago #2 by Karl Faller

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

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago #3 by Robert van der Hulst
Stefan,

You should be able to put the commands on one line if you use the semi colon delimiter:
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:
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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago #4 by Stefan Hirsch
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

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago - 7 months 3 weeks ago #5 by Mathias Håkansson
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
});
Last edit: 7 months 3 weeks ago by Mathias Håkansson.

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago #6 by Robert van der Hulst
Mathias,

Yes. Try this (X# core dialect)
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:
FUNCTION Start AS VOID
	LOCAL nVal AS LONG
	TryCatch ( { => 
			? nVal := nVal / nVal  
			})
	Console.ReadLine()
	RETURN
Robert

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
7 months 3 weeks ago #7 by Robert van der Hulst
Mathias,

And to make it even more "sexy", you can also use the preprocessor to hide the lambda syntax:
#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
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.