Static arrays syntax

After my previous post about the perfect vector/array syntax, I’ve been wondering about static arrays. That means fixed-size arrays that are usually allocated on the stack.

Currently I’m leaning on the following syntax for vectors (dynamically resizable arrays):

[Tester] testers

So what about those static arrays? Maybe something like:

[Tester, 4] testers

Hmm. Is that pretty or is that ugly? I don’t know…

One option would be to do what D programming language does:

//dynamic arrays:
Tester[] testers
//static arrays:
Tester[4] testers

That surely looks more familiar and traditional. But is it really any better? And is there a reason to change it? The freshness of the newer syntax? All the lines of D code, that I’ll have to rewrite with the new array syntax? But now I’m forgetting my previous posts arguments that led to this new syntax in the first place:
Rae has “opt” and “val” and “link” storage/type specifiers which affect what properties a “reference” has. When we do something like:

protected opt Tester[] testers

It is not clear what that opt refers to. That’s why our arrays also need to be prefix rather than postfix.

So, again, maybe this could still be the best syntax for Rae:

//values are default now: val[val Tester] testers
[Tester] testers
//so, static array: val[val Tester, 4] testers would be:
[Tester, 4] testers
//link to a static array containing values:
link[Tester, 4] testers
//and the most dynamic of all arrays,
//the optionally existing resizable array of optional objects: 
opt[opt Tester] maybeTesters

P.S.
How about for loops then:

for(ref Tester a_tester, testers)
{
  a_tester.doSomething
}

And should that “ref” be automatic for function arguments and foreach loops such as this one. So you’d only have to define it for arrays of opts (use link to traverse it) or arrays of links (again use link to traverse).

Choosing the perfect array/vector syntax… Vote in the comments!

In the Rae programming language arrays are going to be implemented using C++ std::vectors. And I’m also thinking of implementing static arrays as std::array, but I’d like them to have the same syntax, and be interchangeable in function arguments, so that requires some more thought…

But what would be the perfect syntax for arrays?

As a reminder you create normal variables like this:

val SomeClass someObject
link SomeClass link_to_ob -> someObject
opt SomeClass someOptObject -> null

So the approach is to use more words and less special characters. But I might want to change this approach as it comes to arrays…

Here’s how arrays are created in the related languages:

vector<Tester> testers; //C++
vector<Tester*> testers; //C++
vector!(Tester) testers; //D
Tester[] testers; //D
Tester testers[];
[Tester] testers //Swift and maybe some other languages...

And here are the candidates for Rae. Feel free to propose more.

array val Tester testers

arr val Tester testers

vec val Tester testers	

vec[val Tester] testers

vec(val Tester) testers
	
[val Tester] testers
	
val Tester[] testers

Vote for your favourite in the comments. 🙂

Somehow, even though otherwise we are using words, the words start to clutter things pretty fast. If we’d want a protected optional array which holds objects by value, we’d write:

protected opt vec val Tester testers

And that would pretty much hide the “vec” and the fact that we have an array. Syntax highlighting might help there, but it still would be hidden.

For some reason I really like brackets in array syntaxes. So currently I think this looks the best, and it neatly looks sort of like a template too:

[val Tester] testers

And it is easy to parse, because the first character in the line is a bracket, so we’re waiting for an array.

So, continuing that thought the [] would implicitly be

val vec[val Tester] testers
//and a link to an array would be:
link [val Tester] linkToArray

And templates would then implicitly be usually val, unless stated otherwise.
//A related note: template syntax with brackets?

vector[Tester] testers
signal[void, int, float] mouseClicked
//But signals deserve a better syntax.

But this “implicitly val” leads to the question, shouldn’t everything be implicitly val? Then we’d really be close to C++ like in our original example of Rae syntax:

SomeClass someObject
link SomeClass link_to_ob -> someObject
opt SomeClass someOptObject -> null

//And our array syntax for value array 
//with value types would be just like in Swift:
[Tester] testers

It is slightly more painfull to parse, as the joy of declaring everything with reserved words beforehand, makes parsing so easy. But maybe I could manage with this syntax…

Any thoughts? Any readers? 🙂

Why do we set pointers with the assignment = operator? Shouldn’t it just copy?

In C++ the assignment operator is a bit strange regarding pointers. I admit that C++’s pointers are very value oriented, but anyway it seems strange to point pointers to objects with the assignment operator (=).

Tester tester_val
Tester* tester_ptr

//value copy
tester_val = *tester_ptr;
//set pointer to object
tester_ptr = &tester_val;

And other members of the language family (Java, C#) have the same problem, and in fact, it is even worse with those two, as a reference and assigning it with the = operator is the default case. (But in C++ you can use value types, and in C# structs…)
I just checked and the web is filled with beginner programmers strugling with these concepts.

So, in my opininion, a language should have a separate assignment (e.g. copying) operator and a separate set pointer (or reference) operator.

Let’s see how it would look in Rae code:

If we’d have a value type object and a link (a non owning reference):

val Tester tester_val
link Tester tester_lnk

Assignment (or copy) operator

tester_lnk = tester_val //segfault because we're trying to copy by value to null.

Actually we’d need to ? it because links are optionals too. So the former shouldn’t even compile. So the correct line would be something like:

tester_lnk? = tester_val

But that will still not run, because link is null currently. 🙂

And a set reference operator could be one of these (or propose a better one in the comments!):

tester_lnk -> tester_val
tester_lnk @ tester_val
tester_lnk @= tester_val
tester_lnk -@ tester_val
tester_lnk +@ tester_val
tester_lnk =& tester_val
//Hmm. This looks familiar.
//Hey that's C++!! Like this:
tester_ptr = &tester_val

I personally think the nicest one of these is the -> as it is instantly recognizable as the “point to” -operator. The bad part is, that it looks like the C++ pointer dereference operator, but since we’re not using it anywhere else, it doesn’t matter.

Let’s look at it one more time, to see if it really looks good or not (as the code is going to get filled with these).

val Tester tester_val
link Tester tester_lnk -> tester_val

What do you think.

P.S. This change also needs another change. The compare references operator. Which is “==” (in C++, Java) and “is” in D. So we could use one of the following:

if( tester_lnk <-> another_lnk )
if( tester_lnk <=> another_lnk ) //looks too similar to <=
if( tester_lnk @ another_lnk )
if( tester_lnk is another_lnk ) //ok, but doesn't look like an operator.

P.P.S. Hmm. A counter argument to this reasoning is this:
We are already removing the already mentioned pointer dereferencing operator

some_ptr->callSomething()

, and just using the dot

some_ptr.callSomething()

everywhere.
We are doing this so that we could just easily change from a pointer type to a value type when needed, without having to replace all our

->

with dots.

So, how does changing the assignment to mean kind of like the same thing with pointers and values affect this code reuse? And the fact that we’ll be mostly using opts and links anyway, which need to be

if(tester_opt)

or

tester_opt?.callSomething()

anyway, if we want to use them. So doesn’t

//Can we get this to show:
?.

serve the same purpose as

->

, but it just isn’t enforced in any way on compile time in C++???

Language design is interesting.

Starting git and github

I did not proof read this post! 🙂

I finally started using git again.

I tried using it some five years ago, but it was so user hostile, that I soon quit. First days using it were ok, but I had a few months break and didn’t remember anything, and so I eventually just gave up.
Now it’s time for another try. It seems the documentation is much better now, and there’s many more sites using it, than five years ago.

Git has one of the most user unfriendly interfaces of all time. It is inconsistent. But I’ll try to use it anyway.

There are probably great tutorials somewhere, and this is not one of them. I didn’t read a tutorial. I just googled every step. That’s why I ran into (small) trouble.

I first created an account for github some months ago. There are also other sites offering similar services. None of them are actually needed to use git, but it makes it easier when you can push your code to some external service and have it stored there.

Then I created a repository. Just a few clicks. Choose licence. MIT. It might have been a good idea to add a .gitignore file, but as the user interface for that wasn’t so clear, and I didn’t know what that was at the time, I did it later, and it was still easy.

Ok. So the trouble came when I did my first push. It had a wrong user name. It actually had the username of my computer account, which is just silly.

Here’s some quick notes what I should have done:

1. Cloned the repository from github to my local computer with:

git clone https://github.com/jonaskivi/rae_programming_language.git

It creates a new dir with the project name, so you don’t have to create a dir beforehand.

2. I should have created a .gitignore file at this point to ignore hidden OS X files. Such as .DS_Store.
Check to see if you have it already with listing all the files:

ls -a

Create the file like this (or edit it if you already have it). echo used together with > will output the text “.DS_Store” into the file .gitignore, which is then also created.

echo .DS_Store &gt; .gitignore

You can also create it with a text editor, but I liked the above solution. I should really try to actually learn the command line. But not vim. Never vim. Today I learned how to quit vim!

3. I should have changed my author name and email. But nobody told me to do it. Here’s how. Change yourgithubusername to your github username.

git config --global user.email "yourgithubusername@users.noreply.github.com"
#inside your git dir, just in case, and if you need different emails for different projects.
git config user.email "yourgithubusername@users.noreply.github.com"
git config user.mail
#OUTPUTS nothing on my machine. Even though the internet says it should.
#instead I checked it with the following (~/ is you home dir.)
cat ~/.gitconfig
#OUTPUT: [user]
#OUTPUT: 	email = yourgithubusername@users.noreply.github.com

4. I should have changed default git editor from vim to nano. Nano has onscreen documentation. Vim does NOT.

git config --global core.editor "nano"

5. I copied all the files and directories that I wanted to be in this repository. With a file manager or however you want to do it.

6. Add all the files with

git add .

7. Check what’s happening with

git status

8. If there are files that shouldn’t be there. I did this. There could be some better commands with HEAD in them.

git rm rae_compiler/rae_compiler/.DS_Store 
#OUTPUT: error: the following file has changes staged in the index:
#OUTPUT:     rae_compiler/rae_compiler/.DS_Store
#OUTPUT: (use --cached to keep the file, or -f to force removal)
git rm -f rae_compiler/rae_compiler/.DS_Store 
#OUTPUT: rm 'rae_compiler/rae_compiler/.DS_Store'

So I had to use the -f flag to force it. I don’t like to force things.

9. Commit to your local git master branch thing.

git commit -m "Added ugly compiler code."

10. To push to github. (Only works if you cloned from there. If you didn’t, you’ll somehow need to link it.)

git push origin master

——————————————————————————-

11. If you forget the -m message thing, you can quit vim by hitting the escape-key on your keyboard, and typing “:q!” to quit without saving.

12. If you’ve already made the mistake of not changing your author. Here’s how to destructively change history. Don’t do it if this is not your project, or something. I didn’t understand the script on github. This one I could understand. Franz Kafka is the old false author name I had.

git filter-branch --commit-filter '
         if [ "$GIT_COMMITTER_NAME" = "Franz Kafka" ];
         then
                 GIT_COMMITTER_NAME="your_new_name";
                 GIT_AUTHOR_NAME="your_new_name";
                 GIT_COMMITTER_EMAIL="your_new_name@users.noreply.github.com";
                 GIT_AUTHOR_EMAIL="your_new_name@users.noreply.github.com";
                 git commit-tree "$@";
         else
                 git commit-tree "$@";
         fi' HEAD

And then tried:

git push origin master
To https://github.com/jonaskivi/rae_programming_language.git
 ! [rejected]        master -&gt; master (non-fast-forward)
error: failed to push some refs to 'https://github.com/jonaskivi/rae_programming_language.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Rejection feels so bad sometimes.

I did “git pull”. It asked me about the message. But I had the Vim default editor. I googled how to quit it! Hit “escape key” and type “:q!” to quit without saving.

git pull

Probably you can put the message in there with -m. Didn’t try it yet.

#OUTPUT: error: There was a problem with the editor 'vi'.
#OUTPUT: Not committing merge; use 'git commit' to complete the merge.

Maybe it was my/vims fault that it didn’t merge.

git commit -m"Trying to fix git author :("

Committed the merge for me.

git push origin master

Pushed it to github. And my name was hopefully fixed, but I’m not sure. The old name still shows up in Github commits though.

End of post. Hope you found what you were looking for.