Hello,
I am trying to create a loop which do some stuff and when finished sleeps until the next iteration at the top of the next minute.
I have the below code but I get a type error when trying to sleep.
import times, os
let minute = initDuration(seconds = 60)
let dt = getTime().utc
var dt2 = dt + minute
dt2 = initDateTime(dt2.monthday, dt2.month, dt2.year, dt2.hour, dt2.minute, 0, utc())
let dur = dt2-dt
let dt4 = getTime().utc
let sleeptime = dur.seconds*1000 + dur.milliseconds
echo("sleeptime: ", sleeptime)
sleep(sleeptime)
let dt5 = getTime().utc
echo("dt4: ",dt4)
echo("dt5: ", dt5)
Produces:
playground3.nim(17, 6) Error: type mismatch: got <int64>
but expected one of:
proc sleep(milsecs: int)
expression: sleep(sleeptime)
> exit status: 256
If I comment out the call to sleep(), and run. I can take the resulting sleeptime and manually place it in the code such as
sleep(16321)
And it works just fine.
I do not understand why it doesn't work.
Any help and wisdom greatly appreciated.
Thanks.
Problem sleeping
More sports, spend more time outdoors, no bright artificial lights in the evenings.
About the programming problem: Convert the int64 to an int with sleep(sleeptime.int).
Look at the error and what it tells you:
Error: type mismatch: got <int64> but expected one of: proc sleep(milsecs: int)
Your sleeptime is int64, and sleep procedure expects an int.
The easiest way to fix this is to convert sleeptime to int:
sleep(sleeptime.int) # the same as sleep(int(sleeptime))
@def
Unrelated. Would love to see more Nim blog posts from you :) I am assuming you are still actively using Nim as you are active here.
Would love to see more Nim blog posts from you :) I am assuming you are still actively using Nim as you are active here.
Nope, just still have the forum in my RSS reader and couldn't resist to reply to the funny title. Others are writing good articles though.
Thanks for all the replies.
I thought it was a funny title also. I didn't have a better one.
I looked for how to convert int64 to an int, but I did not find one.
How would I discover this proc? I don't currently use a magical IDE like VSCode. I have not found it within me to use it.
Thanks.
How would I discover this proc?
https://nim-lang.org/docs/manual.html#statements-and-expressions-type-conversions
The manual answers almost all questions.
Thanks. I do need to read the manual. However, nothing I read there leads me to understand that there is a .int procedure that can be applied to an int64. Everything I have seen about converting to an int is a floating point number.
I come from Smalltalk and Python and am used to an environment I can explore. In Smalltalk everything is available all the time. Discovery is relatively easy.
How do I find the standard procedures available to operate on any type? In this case int64?
Where do I find
proc toInt(i: int64): int =
... convert int64 to int
...
# or
proc int(i: int64): int =
...
I would love to be able to discovery the answer to the question I posted on my own and reduce the frequency in which myself or someone else asks a question whose answer is discoverable.
I hope that makes sense.
Thanks again for the help and replies.
I would love to be able to discovery the answer to the question I posted on my own
When you will have used Nim at least for a few hours you will be able to do this.
Most people start reading the official tutorial, there your question is already answered:
https://nim-lang.org/docs/tut1.html#basic-types-type-conversion
Of course you may also read some of the other beginner docs, we have many now.
And you may try to learn understanding Compiler messages, it told you what was wrong:
got <int64> but expected proc sleep(milsecs: int)
If you think that is not clear enough -- watch C++ gcc messages, maybe when you try to use CGAL lib with all its templates. That can be really confusing.
But why do you want to use Nim at all when you are happy with Smalltalk and Python? My observation is, that from time to time some people with only some basic Python (or similar beginner language like Basic dialects) knowledge come to Nim, ask some questions, and vanish again after a few weeks. I do not really know why they come, and why they vanish. I have a few suspicions, but not many facts.
In case you want to continue to use Nim, you may also consider using IRC or some of its GUI clients like Gitter, many beginner use that channel to get fast help for questions.
Thanks for the reply.
It has been awhile since I read the tutorial.
However, when searching https://nim-lang.org/docs/lib.html
I do not find the int() procedure. I do not find any procedure answers the question of how to provide the right type.
Yes I understood the error message. It was readily apparent. It was getting from where I am to where I want to be that I could not find a path.
My question is and has been is there something I am missing from the standard documentation or is the standard documentation at present not at a place to provide answers like this.
I still do not see where this is defined. And this is for a question we know the answer to. Let alone a question that I do not know the answer and have not asked.
Nim has different challenges with regard to discoverability. In your OO languages you generally find all methods associated with a Type in the class definition. As they generally lack the ability to modify the class and add methods anywhere else. Smalltalk is a little different in this regard.
Regarding Smalltalk and Python. I am a long time user of both. Python longer, but Smalltalk more. I enjoy Pharo. I am able to do my app in Pharo and may do so. I have this app partially in Nim, Python and Pharo. Most recent exploration in Python showed significant performance problems. So I wanted to explore Nim some more.
Nim is very different from Pharo or Python. There is much I like about Nim. The things I don't like are not necessarily problems with Nim, but rather my lack of experience with Statically typed and statically compiled languages. That is something that I do want to learn over time.
I love the idea of small, fast, memory efficient apps. I hate the current trend towards lazy, bloated and slow apps.
I have explored many, many languages over the course of many years. I am not a professional programmer. I have a day job and a family. So like many people, time is limited.
Nim looks like it hits a pretty sweet spot with its syntax, performance, etc. It's current or past state of maturity has been its most limiting factor for me. I have limitations in skill, and time to acquire said skill. However, Nim is getting close to the place where my skills and its maturity are able to start doing things.
I am one of the people you refer to. Looking at my profile, I have been registered for a little over 3 years with 24 posts. Most of my reasons of stopping my attempt with Nim has been the combination of where I was skillwise and where Nim was maturity wise. But I have come and gone a number of times. I keep checking back. I think there are probably a lot of people like me. And as Nim matures and its ecosystem grows. The ability for people like me to use Nim will improve.
I have also explored Rust, Kotlin, OCaml, C++, C. I really like Nim. I think Andreas has created a beautiful language. I think he made a lot of great decisions. And now that there is a little money behind Nim. I think the future is bright.
Nim is different from what I am used to. But different is not bad, it is simply different. I take full responsibility for the problems being with me and not Nim. I am not assigning any blame
Thanks for the suggestion of the IRC. I had forgotten about that. I am used to mailing lists.
I will check out the IRC channel and try there first unless it is something more permanent which deserves post on the forum.
Again, thanks for engaging in conversation. This is a good community. :)
I would love to be able to discovery the answer to the question I posted on my own
Very right, and Nim is very apt to it.
I do need to read the manual.
Yes, it takes just couple of hours, and answers almost all questions about the language in advance - it's a huge time-saver. Regarding your particular question, it's described there that type names may be used for type conversions - it's like for each type there a procedure is defined with that same name, which converts to that type. That is, int, int32, float, and so on - they all may be used for conversions, just by type name.
I still do not see where this is defined.
And then, when you know, that type names may be used for conversions, as procedures, you may find the definition of int in system module of the standard library - lib/system.nim in your Nim installation. Though nothing special there for int, just a built-in type. :) Just read this module throughout, after the manual - it's automatically imported in every your module, and it will be useful to know what is always available. You may instead read the auto-generated docs for the module, if you feel that to be more convenient. This way for any module you use - except for keywords (for, if, ...), every word available in a module is defined in some imported module, so is not hard discoverable; no implicit built-ins, which just there are - everything is in the sources.
I do not find the int() procedure. I do not find any procedure answers the question of how to provide the right type.
That's because int() is not special. What you are looking at is type conversion.
From the manual (https://nim-lang.github.io/Nim/manual.html#statements-and-expressions-type-conversions):
Syntactically a type conversion is like a procedure call, but a type name replaces the procedure name.
If a var x can get converted to type FOO, you can do FOO(x) or x.FOO to do that conversion. In your example, FOO is int.
Slaps self on forehead.
Okay, I know see what you and the manual are saying. It seems obvious when you see and invisible if you don't.
For whatever Type, such as int, simply using the Type as the function to do the conversion. Ugh!
Such that, originalType.convertedType should work for anything where the conversion is implemented?
Thanks for helping the old guy see the obvious.