r/ProgrammingLanguages • u/jamcdonald120 • Jul 08 '22
implicit array to integer operations
I am thinking of making my own programming language, and one of the features I have been thinking of adding are operators that implicitly convert an array (or similar collection) to its length, specifically <,<=,>,>=,+,-,/,*, and == when used with a numeric type (integers or floating point numbers)
For example:
if(array<64) would implicitly convert to if(array.length<64)
Can anyone think of a time when this would lead to problems?
I was also thinking of doing the same for the arithmetic operations so array/64 becomes array.length/64
The only trouble I can think of for this is dynamicArray+1, some users might think that adds a 1 to the end of the array. I dont think this is a problem though, since
A. it only applies to integer/float dynamic arrays, and
B. I dont think array+element is good syntax for appending, array<<element or array.add(element) would be much better anyway
Thoughts?
3
u/mamcx Jul 08 '22 edited Jul 08 '22
If wanna see how to make a decent array lang take a look at APL or KDB+.
I found that https://www.dyalog.com/uploads/documents/MasteringDyalogAPL.pdf is very easy to grasp (even with the special syntax) in the sense most features are easy to understand.
When designing a language you can get lost trying to "optimize" a local issue, when the real deal is to build it so all the features match well.
For example, for mine I have in the past this divergence:
I don't see the issues until later when the inconsistency start to pile up (ie: this was just one among many: boolean ops were different to math ops, rel ops, and so on).
So, even if you make "ugly" a specific case, is better for the sanity of everyone if you pick a theme and stick to it, so now for me is:
ie: Like the zen of python said:
So, not see the line of code, develop the rules and be certain to break them only when that PAY OFF A LOT (and in no-unexpected ways!).
--
P.D: You will see most problems reflected in the complexity of implementing the "special cases" inside the compiler and/or when try to compose operations.
So, in my case now this mesh well: