nim


Why is my string nil?


I made this simple program that reads characters until the enter key is pressed
var data: string
while true:
var c = readChar(stdin) # read char
case c
of '\r': # if enter, stop
break
else: discard
data.add(c) # add the read character to the string
echo data
But when it tries to echo data, it crashes
> ./program
hello
Traceback (most recent call last)
program.nim(11) program
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
This means data is nil. But every time I press enter a character, it adds the character to data. Something goes wrong, but where?
data is initially nil when you define it as var data: string. Instead you can use var data = "" to make it an initialized string.
The stream stdin buffers all the characters until the newline key is pressed, then it will submit the character(s). I expected the behavior to be reading direct characters.
That means that \r will never be the case, it will try to add a character to data but data is nil, so that fails. I thought it failed at the echo statement.
To demonstrate, this code works:
var data = ""
while true:
var c = readChar(stdin) # read char
case c
of '\e': # if escape, stop
break
else:
data.add(c) # add the read character to the string
echo data

Related Links

Nim: How to prove not nil?
Error: expression has no address
MD5 on an array, array[0..63, uint8] to cstring conversion
Converting a seq[char] to string
Nim: On mac, cannot open standard import
How to compare two typedesc in a template for equality
Is there a “default” file name that can be used for importing modules?
Unable to get StringStream created from marshal to output the data
Initialize a seq of seqs
How to change Nim compiler output file location and name
Compile-time Call Count in Nim
How To Convert Slice To Sequence?
Cannonical way to do circular dependency in Nim
toSeq(some_string) Type Mismatch
Why is there a warning not to use system.cmp[string] for Nim string comparison
What's the the best way to get the current operating system?

Categories

HOME
windows
django
fuelux
developer-tools
github-for-windows
datastore
saml-2.0
pclxl
polling
augeas
jquery-ui-sortable
android-securityexception
siddhi
alamofire
gkturnbasedmatch
magnetic-cards
docx
coin3d
easyphp
sse
sar
dpdk
dataflow-diagram
react-toolbox
pchart
google-api-client
motion-detection
angularjs-components
appirater
fractions
intersystems-cache
winexe
magento-1.8
google-cast
aws-codecommit
rselenium
android-maps-extensions
openwhisk
nested-lists
lubridate
apiary.io
latitude-longitude
apns-php
loading
apriori
lpsolve
knime
signal-strength
oim
optimizely
hammer.js
configurationmanager
vugen
convex-optimization
carrot2
pair-programming
wpas
compiler-design
janus
bootstrap-tags-input
rxtx
cubism.js
redbeard-ios
centrifuge
dup
hadoop-partitioning
halcon
concept-insights
java-money
qt5.2
dllimport
file-move
lazybones
flock
django-urls
include-guards
sapscript
sony-lifelog-api
jsvc
apache-commons-daemon
uncaught-typeerror
iiop
crowd
spy++
genymotion-call
ss7
alternate
jquery-slider
clickbank
multitasking
xmltodict
jeromq
alice
subproject
ikimagebrowserview
screen-size
catransform3d
tournament
mbeans
uideviceorientation
delimited-text
maven-1
binomial-heap
message-passing
contentpresenter
pnrp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App