rzimmerman / kal

A powerful, easy-to-use, and easy-to-read programming language for the future.
http://rzimmerman.github.io/kal
MIT License
394 stars 18 forks source link

delete operator #121

Closed dijs closed 11 years ago

dijs commented 11 years ago

Cannot use "delete" to remove object properties...

rzimmerman commented 11 years ago

I'm a little torn on this one...

JavaScript's delete is quirky and sometimes confusing. For example:

x = function () {return 2};
delete x();

Doesn't throw any errors or indicate any issues (except in strict mode, which Kal doesn't use at this time).

var y = 2;
delete y;

Just does nothing.

Is there something better we can do?

dijs commented 11 years ago

The only reason you should use delete if for object properties, not variables or functions.

For the problem I had, I solved it without using the delete operator by assigning it a value of 'undefined'

cmwelsh commented 11 years ago

There is no equivalent to the delete keyword. The best option is to warn people in the documentation to be careful.

http://perfectionkills.com/understanding-delete/

rzimmerman commented 11 years ago

A couple options would be:

  1. Do nothing and just note in the readme that delete isn't supported
  2. Just support JavaScript-style delete (really easy)
  3. Support delete, but do compile-time checks to make sure they aren't trying to delete variables and are only deleting a property or array index.
  4. Make something "new" that is more useful and less confusing.

I'm ok with number 1 if nobody can think of a use case that really benefits from delete.

I don't like number 2 because I think it's confusing to anyone who isn't already a JavaScript expert.

Number three isn't bad, but I worry people will try things like:

x = [1,2,3]
delete x[1]
print x.length

and expect to get 2.

However, I don't think

x = {a:1,b:2}
delete x.a
print x.a

Is confusing.

For number 4, the best idea I have is:

delete property a from x
delete item 3 from y
delete items 10 to 20 from z

In this case I'd use splice rather than delete for the arrays. I think this would prevent people from even trying things like delete a.

But I'm looking for thoughts.

dijs commented 11 years ago

4 is sick! That would be awesome syntax to have.

cmwelsh commented 11 years ago

Sounds like 4 would encourage the correct usage.