wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.9k stars 552 forks source link

Integers? #127

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi, Lua developers recently added integer value types to their language.

Would they make sense in Wren as well? Are they nice to have in game scripting languages? Or were they added to Lua for clearly other purposes?

munificent commented 9 years ago

I'm not currently planning to support a separate integer type. I chose doubles deliberately over floats because they have enough room in the mantissa for integers large enough for almost all normal purposes. Not having a separate integer type makes the implementation simpler and faster and also makes the overall system, I think, easier to reason about.

I should write this up in detail somewhere, but the basic idea is:

  1. Any operation that requires an integer value (basically things that need an index, like insert() on lists, etc.) causes a runtime error if a floating point value is passed.
  2. Bitwise operations (~, ^, |) explicitly act on unsigned 32-bit values and give 32-bit unsigned results.
  3. For everything else, floating point numbers just do what you want.

The main missing piece is an integer division operator.

I admit having only doubles isn't a perfect solution, but I think it works tolerably well for most cases and the simplicity and performance benefits they deliver are pretty significant.

ghost commented 9 years ago

I'm lacking the expertise to really to have a well balanced opinion, but your answer seems to make sense and Lua seems to target mostly embedded systems and comfortable developers with the extra integer.

The main missing piece is an integer division operator.

What about a general truncation operator instead?

munificent commented 9 years ago

What about a general truncation operator instead?

We definitely want that too, but I find it's tedious to rely on that when you want integer division semantics. You get tired of (a / b).truncate all over the place.

ghost commented 9 years ago

Well, that looks reasonable. Thanks.

MarcoLizza commented 9 years ago

By the way, I'm working on the math operators such as truncate. Will send the push request in the next days.

munificent commented 9 years ago

Sounds good, thanks!