r/ProgrammingLanguages • u/PhosXD • Sep 13 '26
Discussion A cleaner way to access arrays/dicts & call functions?
I've been designing & developing my own language from scratch, one of the core concepts I have baked into it is the unique way you call functions, or access objects.
In almost every programming language the way you access an element within an array or dictionary is like so:
array[0];
dict['a'];I always found it weird looking & a waste of characters. Functions are a bit better but still follow that same style.
function(1);In most languages you have multiple different syntactical ways to do basically the same thing, you are performing an access to some object in order to get a result. A function call, just an access to an object with parameters, grabbing an item in an array, just an access to an object with a single parameter, same goes for dictionaries. Why do we have to have to have different ways of doing the same thing?
The way I went about accessing an object was to literally implement a single "accessor" operator, the object you want to access is on the left & the parameter(s) you want to pass are on the right. Here's how that actually looks:
string:0;
array:0;
dict:'a'; dict.a;
function:1; function:[1,2,3];A lot of people are not going to be a fan of this, it goes against every other language & reduces capability for syntax tricks like list comprehension. But for a language that is going for syntactical simplicity this I think is the right move & in the long term is easier (at least for me) to understand & work with.
If you want to take a look at my implementation the repo is open source https://github.com/phosxd/Ity however it's still very much work in progress. Feel free to open an issue or PR if you find any problems with it.
13
u/yjlom Sep 14 '26
Why not ML-style syntax?
9
u/u0xee Sep 14 '26
Unifying these all as functions makes sense. An array is a function of an index to an element. A dictionary is a function of a key to a value. Clojure has a keyword type specialized for being keys in maps, spelled like :name. And keywords are functions of maps, they look themselves up in the map.
As for using colon as the call operator, I guess it’s a bit shorter than a pair of grouping characters. If you like the look and feel, great!
4
u/runningOverA Sep 14 '26 edited Sep 14 '26
to make this you need to support all these use cases :
and then this easy access will need help from classic way as fallback. you will feel the need to support both.
1
u/PhosXD Sep 13 '26
Per AutoModerator's request I hereby confirm that this project did not use an LLM as part of the development process.
4
u/Clementsparrow Sep 14 '26
Why do you want to go for "syntactical simplicity"? I hope it's not for technical reasons in the parser, because that would seem like a very bad reason to me...
Now, if you have reasons for why it would be better for the user of your language, I'd like to hear them. Because I see mostly benefit of having distinct syntaxes for array access, function calls, and member access. For instance:
- if you have a complex function that uses an array, you can scan visually for square brackets to find where it is used in the function.
- in a complex expression you can more easily grasp the structure of the expression if you can distinguish easily nested function calls (parentheses) from simple data access (dot and square brackets). (1)
- there is value in being able to say that `a` is an array because it is followed by an opening square bracket, `s` is a struct because it is followed by a dot, and `f` is a function because it is followed by an opening parenthesis. Sometimes it makes it easier to read code by not having to look for the declaration of the identifiers used in an expression to know what they are.
(1) Operators also help a lot making code easier to read than lisp, but operators could be replaced with function calls for the sake of "syntactical simplicity". Do you intend to do that too?
1
u/marshaharsha Sep 15 '26
Do you allow assignment or insert-or-update for arrays and dicts? If so, that’s an important difference from function calls: you can’t assign to a function call.
I mean the way you can do —
myarray[55] := -999;
wordcount[“hippopotamus”] += 1;
— and the former will write, and the latter will either insert or read-modify-write. I think these special abilities justify syntax dofferent from function-calls syntax.
3
u/miheb1 Sep 14 '26
i think the [] indicates a pure native memory access, so it implies "just a pure access", meanwhile () indicates some functiony or mathy thing.
imo, i would design it like: instead of foo()
you can just do foo
if all parameters are optional and ommitable, sounds terrible but this gives you some c# properies out of thr box
2
u/busres Sep 14 '26
So what does something in a traditional syntax like
myFunction(a[b][c], d[e[f]])look like in your syntax?From looking at move_data.ity and some of your code here, it looks like
thing:'a'might either be an accessor for an item in a container or a function call ofthingwith one parameter, depending onthing. Is that correct?I found myself (admittedly as a new reader) having to stop and think my way through the implications of almost every line of code, trying to track the necessary context to understand what was happening.
It seems like a related concept to something I learned from my own syntax-reducing language; namely that it can make the language harder to scan, at least if you're used to looking for "traditional landmarks".
My case might be particularly extreme though – everything, including assignment, mathematical operations, flow control, function and interface creation, etc. is just messages to objects, and so uses identical syntax.
In my case, it has reinforced the importance of visual layout for human readability (even though it has zero parsing impact).
2
u/jcklpe Sep 15 '26
Neat. I like your approach about trying to think about it structurally rather than getting diverted by convention.
1
u/busres Sep 15 '26
A couple of additional thoughts:
The defined
mergebehavior seems dangerous, since new versions of modules might break existing code if they add a name that's already in use in an importing (merging) module. Is there some reason not to just let the local (importing module) name prevail within its own scope instead of generating an error? Or at least downgrade to a warning?Many code editors give you matching-pair "punctuation skills" for free. Do you have an LSP, or are you planning one, to help users jump between
/;and the matching statement? (Might be an issue for extended, complex, compound accessors too.)1
u/TheRealUprightMan Sep 15 '26
I did a syntax that avoids all reserved words! Everything is a method. So ...
This is an "if" method with two parameters being passed, 'do' and 'else'. Method calls propogate from R to L.
Takes the string, 'file' turns the string into a filename, 'audio' makes an audio object from the file, then we pass 'play' to the audio.
Or :
It's not a function call. It's sending the 'min' function to a literal array, which returns the smallest value in the array.
As for properties, dot indexing and function calls are similar. Dot functions are a variable lookup, so literal names get prefixed, and if a dot operator finds a method, it won't be executed. Method calls are literal names that resolve to hash keys, but if a non-scalar is found, it executes.
The first assignment below is a method call, so if an executable is returned it's excuted until you get a scalar back. The second example explicitly does not execute code.
The # is a literal hashkey, not a variable name.
The index operator is different because it is intended for container storage (arrays, hashes) where indexing the data needs to be different from the properties of the container itself.
It's kinda weird, method first, one of those back shelf projects I always wanted to do and slowly getting back to it.
1
u/Relative-Rooster-945 Sep 15 '26 edited Sep 15 '26
While I think it's interesting, I've seen this explored before and I think the appeal of uniformity overlooks or idealizes the way an actual human parses information. In this case the fact is that it's usually quite useful to visually differentiate accessing data vs performing computation. There is in fact also a semantic difference between
array[i]vsfunction(x), that isarray[i]is an "lvalue" or "assignable" in imperative languages. It describes a location rather than simply a value. I think a language that attempts to unify the two will effectively have to hide the differences in its implementation and my guess is it will ultimately prove impractical.Edit: For functional languages it might work. Accessing linked lists by index is more of a computation than simple memory access anyway, so it might make sense there.
1
u/tesfabpel Sep 15 '26 edited Sep 15 '26
SmallTalk?
https://en.wikipedia.org/wiki/Smalltalk#Messages
acctA transferTo:acctB value:1000array at:0(example implementation)Objective-C?
[acctA transferTo:acctB value:1000];[array at:0];(example implementation)0
u/augustss Sep 14 '26
You're in very good company. Backus's FP also used :