Hello everyone :)
I'm somewhat new to Nim, and have a question about Option[system.bool]... What is it? Sometimes I get errors saying type mismatch: got <bool> but expected 'Option[system.bool]', and when I look at the docs (https://nim-lang.org/docs/options.html#Option) it doesn't quite explain it, or if it does I am either not seeing it, or not understanding it... An example can be this snippet of code:
let q = ChatPermissions(
canSendMessages: false,
...
)
I get that error in the above snippet of code, but I have no idea how to make it become true/false, it doesn't make sense, what is Option[system.bool], and what makes it different compared to regular booleans?https://nim-lang.org/docs/options.html
The Option type is used when a value may be missing. It's a more explicit alternative to nil.
In this case, the value should be one of none(bool), some(false) or some(true). But I'm not sure what it's meant to represent.
you will sometimes see types in error messages in their fully-qualified module.type form. so system.int or system.bool are exactly the standard int, bool types.
it looks like you're trying to use telebot? if my guess is correct, there's an example of how to set chat permissions in the repo here
import ../telebot, asyncdispatch, logging, options, sam
from strutils import strip
import ../telebot/utils
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
addHandler(L)
const API_KEY = slurp("secret.key").strip()
proc commandHandler(b: Telebot, c: Command) {.async.} =
let perms = ChatPermissions(
canSendMessages: some(true),
canSendMediaMessages: some(true),
canSendOtherMessages: some(true),
canAddWebPagePreviews: some(true))
var json = ""
marshal(perms, json)
echo json
discard await restrictChatMember(b, $c.message.chat.id, 50535480, perms)
discard await getChatMember(b, $c.message.chat.id, 50535480)
when isMainModule:
let bot = newTeleBot(API_KEY)
bot.onCommand("perms", commandHandler)
bot.poll(timeout=300)
Thank you! You are 100% correct, I was trying to use Telebot, and I didn't think to even look at the examples!
"you will sometimes see types in error messages in their fully-qualified module.type form. so system.int or system.bool are exactly the standard int, bool types."
Could you please explain further?
If you use a symbol from a module, you can generally use it unambiguously, e.g.
import tables
var myTable:Table[int, string]
But you could specify more strictly which module the type is defined in:
var myTable: tables.Table[int, string]
And sometimes people like to force all imported symbols to be explicitly defined, with e.g.:
from tables import nil
bool is a type defined in a module, like any other. You could even redefine it if you were particularly psychotic:
evil.nim
type bool = enum
false = 3, true = 7
and the two definitions could coexist in the same file, if you specified whether you meant evil.bool or system.bool