beanshell icon indicating copy to clipboard operation
beanshell copied to clipboard

Arrays of boxed primites should not be initialized

Open opeongo opened this issue 2 years ago • 8 comments

The following code defies all expectations and breaks code that expects Java semantics: https://github.com/beanshell/beanshell/blob/5e54a86566ff81e20af75cb221d534ba956a7709/src/main/java/bsh/BSHAllocationExpression.java#L305

Can you explain the rational for initializing arrays with default values? BeanShell is a Java-type language, and this is so different and so unexpected. Why do this?

opeongo avatar Jun 29 '23 16:06 opeongo

Not trying to be confrontational, but this is so different from expectations that I don't see how this fits with BeanShell/Java model. I really think it is worth revisiting this one.

opeongo avatar Jun 29 '23 16:06 opeongo

I added a PR to fix this issue. Discussion welcome.

opeongo avatar Jan 23 '24 23:01 opeongo

If I understand you correctly you want arrays declared as box typed variables, to initialize with null as default value instead of their primitive default values?

Can't remember the reasons now, but it does seems intuitive since we auto box and unbox, and auto narrow and widen numbers.

Need more time to consider this.

nickl- avatar Mar 21 '24 10:03 nickl-

It allows us to do things like:

BeanShell 3.0.0-SNAPSHOT.5569
bsh % Integer[] bi = new int[5];
--> $1 = {0I, 0I, 0I, 0I, 0I} :Integer[]
bsh % int[] pi = new Integer[5];
--> $2 = {0I, 0I, 0I, 0I, 0I} :int[]

nickl- avatar Mar 21 '24 10:03 nickl-

Ok still works with #760

BeanShell 3.0.0-SNAPSHOT.5570
bsh % new Integer[5];
--> $0 = {null, null, null, null, null} :Integer[]
bsh % int[] pi = new Integer[5];
--> $1 = {0I, 0I, 0I, 0I, 0I} :int[]

There must've been a reason for doing this, it will come to me.

nickl- avatar Mar 21 '24 10:03 nickl-

null is not 0. null means we don't know the value. Automatically converting to zero is a mistake.

BeanShell was designed as an extension language for Java. Breaking semantics this way is a bad idea because it will surprise people (it surprised me when this change broke one of my scripts and I had to spend time debugging it).

And why? What benefit is there to breaking things this way? I don't understand your plan.

opeongo avatar May 02 '24 17:05 opeongo

I agree, the BeanShell philosophy is "Works like JAVA but does not break like JAVA", null initialization is not considered a break.

nickl- avatar Aug 10 '24 10:08 nickl-

I agree, the BeanShell philosophy is "Works like JAVA but does not break like JAVA", null initialization is not considered a break.

So, you agree that null is not zero? Or you agree something else?

This is broken, it should throw an exception rather than changing semantics:

> BeanShell 3.0.0-SNAPSHOT.5570
> bsh % new Integer[5];
> --> $0 = {null, null, null, null, null} :Integer[]
> bsh % int[] pi = new Integer[5];
> --> $1 = {0I, 0I, 0I, 0I, 0I} :int[]

opeongo avatar Aug 10 '24 22:08 opeongo