Ask coding questions

← Back to all posts
Syntax for arrays?
DynamicSquid

I'm a little confuzzled on what my syntax for my language should be. Mainly when it comes to arrays.

I don't know if I want predefined functions, or built in syntax. Like this:

The built in syntax definitely looks shorter, but it doesn't really look clean when you're working with an r-value array:

I personally prefer the built in functions for r-value arrays, but the built in syntax for variables.

So which one should I chose? I can't do both

Voters
programmeruser
DynamicSquid
Comments
hotnewtop
xxpertHacker

Totally random, but I barely remembered this.

You might be interested in this piece of knowledge since you're making an interpreted, memory-managed language.

JavaScript implementations, ex: V8, do not use array-like structures to store strings, instead, they use what are known as "ropes".

Some random search results:

The concept can be applied to arrays as well as strings.

DynamicSquid

@xxpertHacker Oh that's interesting, never heard of them before.

Well that took me down a data structures rabbit hole

xxpertHacker

@DynamicSquid I had already conceptually thought of the idea before, but I guess it already existed, and it had a name, and it was already in use. I guess my thoughts aren't that original :(

Data structures are weird at times, I've heard of plenty, but there's an infinite more that exist.

DynamicSquid

@xxpertHacker yup, you're right

maxina

Not sure what language this is, Ruby, maybe?

DynamicSquid

@maxina it's my own :)

maxina

@DynamicSquid So, you are asking for ideas to use for syntax?

DynamicSquid

@maxina yeah, but i figured out already

xxpertHacker

I think dynamically reallocating arrays is just a bad idea.
Unless... you implement arrays as linked lists, in which case I recommend syntax.

DynamicSquid

@xxpertHacker This language is dynamically typed, similar to Python, so arrays are just another value. All the memory management is done behind the scenes.

xxpertHacker

@DynamicSquid Yes, it's behind the scenes, but it's obviously still happening.
You have to see how I treat and think of languages like JavaScript.

"foo" + "bar"; Creates a memory allocation. You might not think of it, but I know it's happening.

xxpertHacker

@DynamicSquid Also, it's safer to give arrays a type, that's what I'm going to do if I create a language.

DynamicSquid

@xxpertHacker Yeah, I'll probably still assign a type to them

DynamicSquid

@xxpertHacker Also, should arrays be implemented as SLLs?

xxpertHacker

@DynamicSquid And "SLL" refers to...?

DynamicSquid

@xxpertHacker Oh, singly linked list. I assume that's what you mean when you said "linked list" but I guess I forgot about doubly linked lists

xxpertHacker

@DynamicSquid Nevermind, the first 'S' in SLL threw me off.

It depends, you can save memory depending on the specific situation.
Usually, you might lose on memory though.

Appending and prepending are always very fast operations.

Iterating is slow.

Take, for example, concatenation between three arrays, ex:

If you were required to have contiguous blocks of memory, then you would have to allocate memory by summing the lengths of all of the individual arrays, then copy the memory into the new array, and assign the variable.

If instead, it was implemented as SLL or DLL, you would simply modify the value that the first array pointed to, and so on. No copies, no reallocations, no variable reassignment.

But I hear that, for complex, performance-critical operations, the performance is terrible.

Ex: Haskell uses LL strings and is a compiled language, but, Python uses blocks, and is interpreted, yet is more performant.

DynamicSquid

@xxpertHacker Ah okay. I'll still have to think about whether to sacrifice searching speed or insertion speed.

fuzzyastrocat

@xxpertHacker I'm interested in your array-as-type idea. How would that work for a function which needs to accept an array of any length?

xxpertHacker

@fuzzyastrocat Now that's where things change.

If you include the length as part of the type, then it is an intricate part of the type, you cannot accept int[5] in place of an int[4].

C/C++ already have this concept though:

At that point, you might as well just degrade to using pointers.

In C, it would just automatically cast to a pointer if the function accepts int*.

In a strongly typed language, the case would have to be explicit, likely through slicing, which is what I intend to do.

HarperframeInc

I suggest built in functions. It's easier to implement and much more readable.

Bookie0

idk for your question, but just a suggestion:

if you already don't have it, you could also have

for inserting elements in the front as well lol :)

DynamicSquid

@Bookie0 But we have

Bookie0

@DynamicSquid hum well ok

fuzzyastrocat

Entirely depends on your language paradigm. If it's OO in some capacity then builtin functions is the way to go. If it's not, then the custom syntax might be a better option. However, even if your language isn't OO you could also make it so that x.y(<params>) is sugar for y(x, <params>).

You could also go haskell-style and make it so that custom syntax is basically just defining another function. I really like approaches like this since custom syntax is so arbitrary and not extendable.

RYANTADIPARTHI

I would prefer class based ones. But it's your opinion.

Coder100

well, it depends on your paradigm, I would definitely go for classes

make sure your arrays aren't pointers that is all

DynamicSquid

@Coder100 Yeah, my language won't have pointers. But which looks cleaner in your opinion though?

Coder100

the class based one @DynamicSquid

Coder100

@DynamicSquid but really you should have both, like:

Coder100

and plz no access more like slice @DynamicSquid

DynamicSquid

@Coder100 Hmm... I'm not really a big fan of syntactical sugar. Cause if I have 2 features that do the same thing, people will use the different features, and then the code won't be consistent.

Also if I change it to slice:

My OCD will kill me

Coder100
Coder100

ok fair enough, but do make sure that there are methods so that things are consistent, because one of these days, there will be a method you just don't want to add sugar to. @DynamicSquid

DynamicSquid

@Coder100 Yeah, you're right

MocaCDeveloper

I'd say built in tbh. It is just so much easier and will become less of a hassle on behalf of the people using your language.

You don't wanna be like C where you need to literally create your own concept of arrays.

Also, Good job on your language. You should attempt to convert it into a compiled language over time! It sure would be a cool compiled language!

DynamicSquid

@MocaCDeveloper Yeah true I guess. But wdym for your second paragraph? Because the functionality for both built in syntax and predefined functions are the same.

MocaCDeveloper

@DynamicSquid

Oh I am just saying like lets say you have neither..then you'll end up like C.

Ye I know predefined and built-in are the same. But the built-in syntax looks better(if I am looking at it correctly)..

MocaCDeveloper

@DynamicSquid

A c like language with built-in methods will make any developer fall in love: Fast compatibility and extreme efficiency!

DynamicSquid

@MocaCDeveloper Oh, it's not C like at all! I think I forgot to update you on this, but for version 4 (coming soon), Night's going to be dynamically typed. It's also going to be really similar to Python, but it will have compile time checks making it safer.

So basically Python but it's safe

DynamicSquid

@MocaCDeveloper Oh, I want this language to be interpreted, but I think that once I get this to a good point, then I'll definitely make another compiled language.

MocaCDeveloper

@DynamicSquid

Oh so you’re totally shifting the concept of the language?

MocaCDeveloper

@DynamicSquid

It's compiled?

DynamicSquid

@MocaCDeveloper Oh, I meant that this language would interpreted, but I'll make another language and that language will be compiled, so I'll have two languages, one interpreted (this one), and one compiled.

And yeah, I'm shifting the concept of the language. I think that now with my language being interpreted and dynamically typed I'll stick to that. I kind of want to make something like Python, but like I said a major difference is the safety.

MocaCDeveloper

@DynamicSquid

Night seems like it is going to be a unique dynamic language!

It is going to have some syntax that of a statically typed language, right? Like:

Like that? Where you define the types of functions and variables?

DynamicSquid

@MocaCDeveloper Nope, they'll all be inferred automatically.

Variables use the set keyword:

And functions use the def keyword:

It's going to be hard to implement type checking for function parameters at compile time, but I think that's doable (I think)

Edit: Oh whoops forgot, there's no semicolons

MocaCDeveloper

@DynamicSquid

Ye I've attempted to do it 3 times lol and I gave up. I'm sure you'll be able to do just fine though. You have a better attention span than me!

Also, stop copying my idea BRO. Me and @realTronsi were creating a language one time and we were going to use the set keyword as a variable declaration like seriously bro stop copying us lol

:D

DynamicSquid
realTronsi
MocaCDeveloper
realTronsi

@MocaCDeveloper alphabet soup?

MocaCDeveloper

@realTronsi

alphabet went bye bye after I got done with it

realTronsi

@MocaCDeveloper NO u ditch the sheep

MocaCDeveloper

@realTronsi

ok stair sis what we doinn

realTronsi

@MocaCDeveloper thought code 26 || suggest ?

MocaCDeveloper
realTronsi

@MocaCDeveloper me thot we do A26 ??

MocaCDeveloper

@realTronsi

I am so confused..what???

MocaCDeveloper

@realTronsi

ye we are lol I am just not gonna get on right now until my next class starts. The teacher usually gives busy work the first 10 minutes of class

realTronsi

@MocaCDeveloper ok sounds good I'll work on it

DynamicSquid

@realTronsi

ok stair sis what we doinn

you guys are siblings?

realTronsi

@DynamicSquid inside joke, ignore lol

MocaCDeveloper

@realTronsi

Ok. I bet you'll manage to receive 200 seg faults

MocaCDeveloper

@DynamicSquid

Lol no me and @realTronsi literally just run together on repl and do a whole bunch of coding projects together.
I seriously don't know where this friendship started between me and him but uhh ye we just tend to work together allot and manage to just talk to each other on literally everyones post lol

Also, stair sis is a inside joke. Don't ask how we came up with it lol

:D

realTronsi

@MocaCDeveloper bet I'll receive more

MocaCDeveloper

@realTronsi

I bet so too

DynamicSquid

@MocaCDeveloper I see... lol xD

MocaCDeveloper

@DynamicSquid

..you see nothing ¯\(°_o)/¯

realTronsi

@MocaCDeveloper

MocaCDeveloper

@realTronsi

Uhm I think you're a bit too ahead of yourself..

realTronsi

@MocaCDeveloper why :(( d can just be for data, kinda like .data in assembly, $ is for cross referencing registers, and @ can be the global register kinda like the main function

fuzzyastrocat

@DynamicSquid It's doable, but very difficult (which is why no language that I know of, outside Eros and Night, do it)

realTronsi
realTronsi

@MocaCDeveloper do u have work agian

MocaCDeveloper

@realTronsi

I will be able to code in a minute

realTronsi
MocaCDeveloper
realTronsi

@MocaCDeveloper too late I fell asleep waiting

MocaCDeveloper

@realTronsi

Sorry :/