How does this translate C#->X#

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How does this translate C#->X#

Post by ic2 »

This C# code:

Code: Select all

	public static void ProcessUITasks()
	// When called, forces the UI to redraw. 
	{
		DispatcherFrame frame = new DispatcherFrame();
		Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate (object parameter) {
			frame.Continue = false;
			return null;
		}), null);
		Dispatcher.PushFrame(frame);
	}
in ILSpy translates as:

Code: Select all

public static method ProcessUITasks() as void
	local frame as DispatcherFrame
	frame := DispatcherFrame{}
	Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ =>
		frame:Continue := false
		return null
	}, null)
	Dispatcher.PushFrame(frame)
This gives Error XS1593 Delegate 'System.Windows.Threading.DispatcherOperationCallback' does not take 0 arguments

Indeed delegate (object parameter) from the C# code is missing. But I am not sure how to do this in X#

Anyone an idea?

Dick
User avatar
Chris
Posts: 4910
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

How does this translate C#->X#

Post by Chris »

Hi Dick,

This should do it, by supplying the parameter as the c# version of the code does:

Code: Select all

	Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ parameter AS OBJECT =>
		frame:Continue := FALSE
		RETURN NULL
	}, NULL)
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

How does this translate C#->X#

Post by ic2 »

Hello Chris,

Thanks, it works now.

I must confess that I have no idea how the syntax is supposed to work; any Lambda expressions or "delegates" in my program are "borrowed" and even now I see your working X# solution I do not understand how you got to this (gone is the delegate, added is the =>).

But main thing is that it works!

Dick
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

How does this translate C#->X#

Post by robert »

Dick,

There are 2 ways to write this:
- A Lambda expression (with the => operator, like you did)
- With the DELEGATE keyword

Code: Select all

Dispatcher.CurrentDispatcher:BeginInvoke(DispatcherPriority.Background, (DispatcherOperationCallback){ DELEGATE ( p AS OBJECT )  {  frame:Continue := FALSE, NULL }, NULL)
The syntax for the DELEGATE expression is

Code: Select all

DELEGATE [ ( Parameters) ] { <Block> }
Where <Block> is :
- a single expression
- an expression list, seperated with commas. The last expression is the return value, like above. All on one line.
- a statement block (starts with a CRLF after the LCurly ({) and also has a CRLF before the RCurly (})
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply