xsharp.eu • Typed parameters in codeblocks
Page 1 of 1

Typed parameters in codeblocks

Posted: Thu May 07, 2020 6:44 pm
by Serggio
I get an error while trying to compile a static codeblock with typed arguments:
Error XS9057. Typed parameters in codeblocks are not supported by the runtime.

But in fact CAVO supported typed arguments in codeblocks pretty fine. There was just a warning, which may be disabled without consequences. And there are lots of advantages when using typed arguments: compiler really does a compile-time check for any type compatibility, allows you to make an early-bound call, etc. It's just the intellisence which doesn't work on those variables, but everything concerning compiling and linking works just as it should.

For instance, such a code works in VO:

Code: Select all

CLASS Test

	~"ONLYEARLY+"
	DECLARE METHOD EarlyMethod
	~"ONLYEARLY-"

METHOD EarlyMethod(nArg AS INT) AS VOID PASCAL CLASS Test

	? nArg

RETURN

FUNCTION doTest() AS VOID PASCAL
LOCAL oTest AS Test
LOCAL cbGood, cbBad AS CODEBLOCK

	oTest := Test{}
	
	 // this compiles with no errors (with a suppressible warning)
	cbGood := {|oTestClass AS Test, nParam AS INT| oTestClass:EarlyMethod(nParam)}

	Eval(cbGood, oTest, 5) // prints 5 as expected

	// and that will not compile due to type incompatibilty of the argument:
	cbBad := {|oTestClass AS Test| oTestClass:EarlyMethod("string")}

RETURN
But this code refuses to compile in X#.

Typed parameters in codeblocks

Posted: Thu May 07, 2020 8:10 pm
by Serggio
Maybe there's a simple way to cast somehow a lambda-expression to a static codeblock? They seem to be of the same nature. That would solve the problem either.

Typed parameters in codeblocks

Posted: Thu May 07, 2020 9:26 pm
by robert
Serggio,
This is new to me.
I did not know that VO allowed this (and I have worked on VO for a few years ...).
I don't think this was intentional.
I'll have to study this and discuss this with the guys.

But since we have multi line codeblocks, this should work (but it does not...)

Code: Select all

 cbGood := {|uTestClass , uParam | 
   LOCAL oTestClass := uTestClass as Test 
   LOCAL nParam := uParam as INT
   oTestClass:EarlyMethod(nParam) 
   RETURN NIL 
   }

Robert

Typed parameters in codeblocks

Posted: Thu May 07, 2020 9:31 pm
by robert
Serrgio,

This works:

Code: Select all

	cbGood := {|oTestClass , nParam |  ((Test) oTestClass ):EarlyMethod( (int) nParam ) }
Maybe we can tell the compiler to produce something like this when you declare typed parameters for a codeblock.

Robert

Typed parameters in codeblocks

Posted: Thu May 07, 2020 10:08 pm
by Serggio
Robert,
That's a good workaround! Thank you!
And if you make it somehow behind the scenes so that the original syntax keeps intact, that would be superb!

Typed parameters in codeblocks

Posted: Fri May 08, 2020 6:36 am
by robert
Serggio,
I have created a ticket for this:
https://github.com/X-Sharp/XSharpPublic/issues/388

Robert