Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 357410

Summary: delegate array can't be initialized
Product: z_Archived Reporter: Jing Qian <jqian>
Component: EDTAssignee: Project Inbox <edt.compiler-inbox>
Status: CLOSED FIXED QA Contact:
Severity: blocker    
Priority: P3 CC: carrollk, mheitz, pharmon, wxwu
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Jing Qian CLA 2011-09-12 15:42:18 EDT
Here is the .egl file

package a;

delegate jingDelegate(i int) end

// basic program
//
program Hello type BasicProgram {}
	
	function main()
		sysLib.writeStdOUt("abc");
		a jingDelegate[] = new jingDelegate[1];
		a[1] = function1;		
		a[1](4);
		
	end
	
	function function1(i int)
		i += 5;
		sysLib.writeStdout("function1 is delegate, i is " + i);
	end
end


It gens and compiles in java, but when you run it, you will get egl.lang.InvalidArgumentException

because it's trying to call delegate.newInstances(), and it does not have that method defined.
Comment 1 Jing Qian CLA 2011-09-12 15:42:47 EDT
This is a blocking issue for EUnit framework, as I use delegate array in the framework.
Comment 2 Matt Heitz CLA 2011-09-12 17:21:52 EDT
This changed as part of the initialization and nullability language changes.  Any type that doesn't have a default (no argument) constructor, such as a delegate, must be declared nullable.  The same goes for arrays of delegates.

There should be an error on the declaration saying that it should be nullable.

The declaration should look like this:

a jingDelegate?[] = new jingDelegate?[1];
Comment 3 Jing Qian CLA 2011-09-14 10:13:28 EDT
while I changed my generated code to use nullable ? to by pass this issue for now. 

I very much question the way this is designed, as it makes the coding a lot more complex when user uses array of objects (delegates, externalTypes, etc..), 

especially for delegates, 

And I can see another issue with this design. 

consider the following:

I have a function defined as 

function myFunc(a jingDelegate[])
  x jingDelegate = somedelegate;
  a.append(x);
end

In order for someone to call this function.

they HAVE TO declare a variable 
v jingDelegate[] = [a, b, c];  
(by the way, this doesn't work yet, there is another defect open on it)

user HAS TO initialize every single element at the declaration time. 
what if the logic is to have the function initialize all of the elements.
Comment 4 Jing Qian CLA 2011-09-14 10:15:34 EDT
while I changed my generated code to use nullable ? to by pass this issue for
now. 

I very much question the way this is designed, as it makes the coding a lot
harder when user uses array of objects (delegates, externalTypes, etc..), 

And I can see other issues with this design. 

- especially for delegates, how do I make to have an empty constructor?

- consider the following:

I have a function defined as 

function myFunc(a jingDelegate[])
  x jingDelegate = somedelegate;
  a.append(x);
end

In order for someone to call this function.

they HAVE TO declare a variable 
v jingDelegate[] = [a, b, c];  
(by the way, this doesn't work yet, there is another defect open on it)

user HAS TO initialize every single element at the declaration time. 
what if the logic is to have the function initialize some or all of the elements
Comment 5 Matt Heitz CLA 2011-09-14 12:32:16 EDT
[I changed the component from MOF Model to Compiler since this is a validation issue.]

Jing, why do you say this design makes coding a lot harder?  As I understand it, the only new thing you have to do is add some question marks.  And after this validation defect is fixed, you'll get a red X telling you where the question marks are needed.

> - especially for delegates, how do I make to have an empty constructor?
You can't.  It doesn't make sense for a delegate to have a constructor.  A delegate is sort of a mini-interface, like an interface with just one function.  So it's an abstract thing that can't be instantiated.

> - consider the following:
Your example function is not really valid.  The parameter's type should be jingDelegate?[] not jingDelegate[].  That means the elements of the array can be uninitialized because they'll default to null.
Comment 6 Paul Harmon CLA 2011-09-15 08:52:22 EDT
I have updated DelegateBinding so that delegates are not instantiable
Comment 7 Lisa Lasher CLA 2011-11-09 14:13:21 EST
This high severity defect was fixed several weeks ago, so I am closing.