I have defined some single character string constants which sit in their own 'chars.nim' file.
const twHat* : string = "^"
const twBar* : string = "|"
const twComma* : string = ","
const twPeriod* : string = "."
const twSpace* : string = " "
const twHyphen* : string = "-"
const twColon* : string = ":"
const twSemiColon* : string = ";"
const twHash* : string = "#"
const twPlus* : string = "+"
const twAsterix* : string = "*"
const twLParen* : string = "("
const twRParen* : string = ")"
const twRAngle* : string = ">"
When I compile I'm seeing a single error but I cannot work out the cause.
: used config file 'C:\Users\XXXXXX\.choosenim\toolchains\nim-#devel\config\nim.cfg' [Conf]
Hint: used config file 'C:\Users\XXXXXX.choosenim\toolchains\nim-#devel\config\config.nims' [Conf]
.....................................................................................................
C:\Users\XXXXXXc\source\repos\AdventOfCode\Nim\nim2022\src\chars.nim(25, 45) Error: type mismatch: got 'string' for '"#"' but expected 'char'
output of nim -v is
Nim Compiler Version 1.9.1 [Windows: amd64]
Compiled at 2022-12-25
Copyright (c) 2006-2022 by Andreas Rumpf
active boot switches: -d:release
Please note that I am an extreme novice when it comes to setting up VSCode for working with nim.
Single character constants would look like:
const twHat*: char = '^'
Thanks for the reply. As you can surmise this is an Advent of Code problem. My original solutions were written in the new twinBasic dialect of VBA. I have a simple VBA macro that does some search and replaces which gets VBA/twinBasic code to 90% nim syntax.
twinBasic/VBA doesn't have char Types hence the single character strings. I actually implemented your proposal before reading the above and all works as it should.
The line is
const twHash* : string = "#"
but as you can see from my response to Araq I've since converted the single character strings to chars and now don't see any errors.a
I have a simple VBA macro that does some search and replaces which gets VBA/twinBasic code to 90% nim syntax.
Nim 3.0 just dropped
Obviously OP confuses single quotes with double this line gives the exact error:
const twHash* : char = "#"
Apologies if I'm sowing confusion. I'm just a complete amateur programmer looking for something to test solutions when VBA/twinBasic takes too long or has memory issues. Nim seemed to fit the bill really well, especially as the syntax allows an almost 1:1 mapping from VBA to nim. I just have to remember that VBA passes parameters by reference and nim by value. It causes some entertainment (late night frustration) on occasion.
This was the original file that was leading to the error message when I it was imported as '../../AoCLib/chars'. There are two blank lines before the first line below.
# \r, \c carriage return
#\n, \l line feed
#\f form feed
#\t tabulator
#\v vertical tabulator
#\\ backslash
#\" quotation mark
#\' apostrophe
#\ '0'..'9'+ character with decimal value d; all decimal digits directly following are used for the character
#\a alert
#\b backspace
#\e escape [ESC]
#\x HH character with hex value HH; exactly two hex digits are allowed
const twNoString* : string = ""
const twHat* : string = "^"
const twBar* : string = "|"
const twComma* : string = ","
const twPeriod* : string = "."
const twSpace* : string = " "
const twHyphen* : string = "-"
const twColon* : string = ":"
const twSemiColon* : string = ";"
const twHash* : string = "#"
const twPlus* : string = "+"
const twAsterix* : string = "*"
const twLParen* : string = "("
const twRParen* : string = ")"
const twRAngle* : string = ">"
const twLAngle* : string = "<"
const twAmp* : string = "@"
const twLBracket* : string = "["
const twRBracket* : string = "]"
const twLCurly* : string = "{"
const twRCurly* : string = "}"
const twPlainDQuote* : string = "\""
const twPlainSQuote* : string = "'"
const twLSmartSQuote* : string = "‘" #ChrW$(145) ' Alt+0145
const twRSmartSQuote* : string = "’" #ChrW$(146) ' Alt+0146
const twLSMartDQuote* : string = "“" #ChrW$(147) ' Alt+0147
const twRSmartDQuote* : string = "”" #ChrW$(148) ' Alt+0148
const twTab* : string = "\t"
const twCrLf* : string = "\r\n"
const twLf* : string = "\n"
const twCr* : string = "\c"
const twNBsp* : string = "\255" #Chr$(255)
which came from the VBA file
Public Const twHat As string = "^"
Public Const twEqual As string = "="
Public Const twLArrow As string = "<"
Public Const twRArrow As string = ">"
Public Const twNoString As String = ""
Public Const twBar As String = "|"
Public Const twComma As String = ","
Public Const twPeriod As String = "."
Public Const twSpace As String = " "
Public Const twHyphen As String = "-"
Public Const twColon As String = ":"
Public Const twSemiColon As String = ";"
Public Const twHash As String = "#"
Public Const twPlus As String = "+"
Public Const twAsterix As String = "*"
Public Const twLParen As String = "("
Public Const twRParen As String = ")"
Public Const twAmp As String = "@"
Public Const twLBracket As String = "["
Public Const twRBracket As String = "]"
Public Const twLCurly As String = "{"
Public Const twRCurly As String = "}"
Public Const twBSlash As string = "\"
Public Const twFSlash As string = "/"
Public Const twPlainDQuote As String = """"
Public Const twPlainSQuote As String = "'"
Public Const twLSmartSQuote As String = "‘" 'ChrW$(145) ' Alt+0145
Public Const twRSmartSQuote As String = "’" 'ChrW$(146) ' Alt+0146
Public Const twLSMartDQuote As String = "“" 'ChrW$(147) ' Alt+0147
Public Const twRSmartDQuote As String = "”" 'ChrW$(148) ' Alt+0148
Public Const twTab As String = vbTab
Public Const twCrLf As String = vbCrLf
Public Const twLf As String = vbLf
Public Const twCr As String = vbCr
Public Const twNBsp As String = "ÿ" 'Chr$(255)
I have continued editing /plating since the original post and currently I'm seeing
const SOUTH : string = "v"
with the "v" highlighted as an error with the error message
type mismatch: got 'string' for '"v"' but expected 'char'
If it helps, I really quite like nim.
What happens when you replace
const twHash* : string = "#"
with
const twHash* : string = "\xHH" # HH = ASCII value of #
?I think I've located the cause of the issue.
The constants
const WALL : string = twHat
const PATH : string = twPeriod
const EAST : string = twLAngle
const WEST : string = twRAngle
const NORTH : string = twHat
const SOUTH : string = "v"
are used in a single location to convert strings to set of x,y coordinates.
the problem test data set is
#E######
#>>.<^<#
#.<..<<#
#>v.><>#
#<^v^^>#
######.#
myData is a seq of strings representing the above dataset this dataset is converted to sets of x,y coordinates using
s.FinalXY = (myData.first.find(twHash),myData.low)
s.Current = (myData.last.find(twHash),myData.high)
for myRow in s.MinRow..s.MaxRow:
for myCol in s.MinCol..s.MaxCol:
**** let myChar: char = $mydata[myRow][myCol] *****
case myChar
of WALL:
s.Wall.add((myCol, myRow))
of NORTH:
s.NorthB.add((myCol, myRow))
of SOUTH:
s.SouthB.add((myCol, myRow))
of WEST:
s.WestB.add((myCol, myRow))
of EAST:
s.EastB.add((myCol, myRow))
else:
discard
the line highlighted with **** had been giving me an error at the [mycol] when myChar was defined as string, so I'd changeed it to char which leads to a mismatch between the constant declarations and the case variable. type.
changing
let myChar: char = mydata[myRow][myCol]
to
let myChar: string = $mydata[myRow][myCol]
resolves the problem with twHash but now leaves me with an error of '
C:\Users\xxxxx\source\repos\AdventOfCode\Nim\AoCLib\src\chars.nim(20, 45) Error: duplicate case label
which is the line
const twHat : string = "^"
If I rewrite the case statement using the tw strings directly
for myRow in s.MinRow..s.MaxRow:
for myCol in s.MinCol..s.MaxCol:
let myChar: string = $mydata[myRow][myCol]
case myChar
of twHash:
s.Wall.add((myCol, myRow))
of twHat:
s.NorthB.add((myCol, myRow))
of "v":
s.SouthB.add((myCol, myRow))
of twLAngle:
s.WestB.add((myCol, myRow))
of twRAngle:
s.EastB.add((myCol, myRow))
else:
discard
All errors go away and I get a successful compile.If you remove the $ from myChar: char = $mydata[myRow][myCol] and convert the constants to chars as well, it should work.
Thanks for giving information about this issue, the confusing error message should be fixed with https://github.com/nim-lang/Nim/pull/21182
Indeed, I continue to explore how nim can be used better. I now have those single character string defined as chars and am experimenting with
type
direction = enum
North = (0, $twHat)
East = (1, $twRAngle
South = (2, "v")
West = (3, $twLAngle)
and I've moved the individual blizzard seq into a Table
s.Blizzards=[$North: newSeq[Point](), $East: newSeq[Point], $South; newSeq[Point](), $West: newSeq[Point}()}.toTable
which is working well, although I couldn't understand why
s.Blizzards = {$North; @[], $East: @[], $South: @[], $West: @[]}.toTable
refused to compile even though s.Blizzards has been assigned the Type of Table[string, seq[Point]]