beanshell / beanshell

Beanshell scripting language
Apache License 2.0
815 stars 183 forks source link

Arrays of boxed primites should not be initialized #747

Open opeongo opened 10 months ago

opeongo commented 10 months ago

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 commented 10 months ago

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 commented 3 months ago

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

nickl- commented 1 month ago

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- commented 1 month ago

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- commented 1 month ago

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.

opeongo commented 2 weeks ago

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.