include tables, sets
type
BS8 = set[int8]
DigSet = array[9,BS8]
T2int = tuple[f:int8,s:int8]
var
FreeCell:HashSet[T2int]
Row_Digits : DigSet
R_Cells : int
var ChkPoints= initTable[int, (HashSet[T2int], DigSet, int)] ()
proc Checkpoint(Id:int) :
ChkPoints[Id]= (FreeCell, Row_Digits, R_Cells)
#[ gives
/usr/lib/nim/pure/collections/sets.nim(62, 3) Error: redefinition of 'KeyValuePair';
previous declaration here: /usr/lib/nim/pure/collections/tables.nim(225, 3)
]#
Is this a bug, my error but an incomprehensible error message or what else?
Thanks, an embarrassing error, but why didn't this get flagged? Now the line
var ChkPoints= initTable[int, (HashSet[T2int], DigSet, int)] ()
gives Error: type mismatch: got <tuple[]> but expected one of: proc (initialSize: int): Table[system.int, (HashSet[BUG.T2int], DigSet, int)]{.noSideEffect, gcsafe, locks: 0.} which I don't understand eitherRemove the space between ] and (: initTable[int, (HashSet[T2int], DigSet, int)]()
But this will continue to bite you in the future because of your sloppy writing style when it comes to whitespace: you have it in places where you shouldn't, you don't have it where it would be nice to have, and Nim encourages/enforces you to care about that stuff.
Also, I would encourage you (again; as I already told you the same on IRC/Gitter) to read some style guides when it comes to naming your variables, etc.
It turns out that Nim has problems with the blank before () in
var ChkPoints= initTable[int, (HashSet[T2int], DigSet, int)] ()
^
That's a pity and hard to find.Oh well, I have coded more than a million lines of code in more than a dozens of programming languages. I have been teaching Pascal/C++ for more than 3 decades at a university.
So one develops one's own style. And it's just a matter of style. I can remember only very few places where white space is significant like between two characters of a composite operator like !=
But I have never encounter a language where a space in front of the calling operator is significant. And even worse, it is not flagged as a syntax error but seems to have a totally different semantic meaning. So, what is the semantic of such a construct. So, it wouldn't hurt if such critical misplacements of white space would produce a warning at least.
We want alignment for instance in this case:
if some_long_expression and
another_long_expression and
another_one:
Of course, we could use parentheses and write, using only indentation:
if (
some_long_expression and
another_long_expression and
another_one):
but this is silly and ugly.
Of course, alignment is not a requirement but it improves readability. And for most languages, indentation is not a requirement either but who would say it is useless ?
Well I never said it was useless, I just don't get the point. Indentation's purpose is to show scope, I'm uncertain if alignment even makes more readable code. Do want to say though that you can omit the () and just write as follows not that I suggest this.
if
10 == 10 or
20 == 30 or
false:
echo "Heh"
Nim's hard choice of "two spaces per indent" is not helping its adoption. It might seem as a pet peeve but for new Nim users with experience in other languages it can be a serious irritation.
There is no such "hard choice" and there is no evidence whatsoever that it hinders its adoption. FUD.
Of course a good editor could fix that...
Well I'm not gonna change the language for people who prefer to use broken tools, sorry.
But there is a good alternative: Let's focus our efforts on a standard tree format for programming languages. We can then store code as trees on the disk and eventually git diff can diff two different trees rather than this comical text based comparison where we're supposed to use more unstructured programming (return) just so that git diff doesn't screw up as badly as it usually does.
@Araq: You have to use gofmt to get it or you need to be conciously aware of tabs vs spaces all the time. Excuse me, but I prefer to spend more time on the algorithmic aspects of my code.
Modern editors are surprisingly good at keeping this complexity away from you. VS-code with Golang extension handles indentation quite well. Most of the time you type ENTER and DELETE, sometimes TAB and very infrequently SPACE for alignment. By default, gofmt runs on every file safe and rarely messes with indentation (it has other annoyances but indentation is not one of them).
How do you set the "tab width" when browsing source code online? When I look at Go code online the excessive indentation is annoying.
Agreed. Online code browsing is a clear step down from a good IDE. Oftentimes I end up cloning the repo and firing up vscode just to just read the code.
There is no such "hard choice" (feel free to use 4 spaces) and there is no evidence whatsoever that it hinders its adoption. FUD.
I bet, if I submit a PR to you against Nim compiler or stdlib with 4 spaces/indent you will rightfully reject it on the grounds of inconsistent code-style.
What I really love about Nim is that it settles decades long disputes in a most organic and inclusive way.
And where is Nim's flexibility when it comes to Tabs vs Spaces? I read somewhere that in distant past Nim had a skin that allowed braces to be used for scoping instead of white space like in C++. Compared to that, allowing TABs for indentation is peanuts.
Indentation's purpose is to show scope, I'm uncertain if alignment even makes more readable code.
No, indentation is certainly not a way to indicate scoping as in old languages, such as Pascal, an IF, a FOR or a WHILE statement doesn’t create a new scope. Even in Python an if, a for or a while statement doesn’t create a new scope. In most languages, indentation is not syntactically required and is in fact only a way to improve readability by making more apparent the syntactical structure.
Now, let’s look at some statement like:
let xx = if expression1: expression2 else: expression3
If the statement is too long, we have to split it. Using only indentation, there is several ways, for instance:
let xx = if expression1: expression2
else: expression3
let xx = if expression1: expression2
else: expression3
let xx = if expression1: expression2
else: expression3
let xx = if expression1: expression2
else: expression3
But the more logical way needs an extra space for alignment:
let xx = if expression1: expression2
else: expression3
One can say that the difference is not so important, but for me this is not the problem. I simply don’t want to be constrained to use indentation only. Syntactically significant indentation is fine for me as it improves readability and, so, this is a good constraint. Using indentation only will be unacceptable: this would be pushing too far away the bondage and the discipline.
For me, tabs have nothing to do in code. Each time I copied Python code containing tabs, its was a nightmare. And having indented and aligned my code even when I used punch cards or text editors such as “ed”, I have some difficulty to understand where is the problem to use only spaces, moreover when modern text editors automatically indent and even deindent.