Here's an example.. let's say I have a File type variable f which is initially unset i.e. I have not yet assigned it an open("file.txt") value.
In Python, I can do something like:
if f:
# code to assign the file object to f
In Nim, doing similar gives:
Error: type mismatch: got <File> but expected 'bool'
So the question is, how doing I do a boolean check on if that File type variable is set to a valid file object in Nim?.. something like if (isValid f):?
https://github.com/Yardanico/nimpylib#use
>>> with_open("some_file.txt", 'r'): # Mimics Pythons with open(file, mode='r') as file:
while not end_of_file(file): # File is automatically assigned to file variable.
print(file.read_line()) # No need for " as file", just path and mode.
# File is closed automatically.
Theres also a clone of Pythons pathlib on Nimble.
For the sake of being exhaustive :).
Is it possible to create an object that is assignable in a parental sense? Essentially, to be able to create a "type" that behaves like a basic type.
type
PyInt* = ref object of RootObj
val: int
default*: int
has*: bool
method PARENT_SET(self: PyInt, value: int) =
self.val = value
self.has = true
method PARENT_GET(self: PyInt) =
if self.has:
result = self.val
else:
result = self.default
var x: PyInt
echo x.has # => false
echo x # => 0
x = 4
echo x.has # => true
echo x # => 4
Essentially, I don't have to ever access x.val. I can, with enough methods for the object, treat it like a plain integer; but it has additional states than a plain int has.
I ask because one of the things I liked about Python was the ability to create new types. True types. I, in fact, wrote a library that created a new structure I called a ROLNE. It acts like nim table except that the key strings were not unique. They automatically created key sequences. aka 'larry["bob"]' referenced the first "bob". But 'larry["bob", 1]' referenced the second one.
Apart from some syntactic similarities nim and python are not really similar languages. Its best not to try and apply pythonic approaches to nim.
For nim its probably more idiomatic nim using an approach which combines composition, proc overloading, generics, and object variants rather than using nim's "object oriented" based approach.
As far as conditionals statements go, nim by default only supports boolean based conditional statements where python will recognised empty lists, None, 0 as false. In nim nil, 0, empty sequence are not boolean types and so they cannot be used by themselves in a conditional statement.
So the following will fail to complile:
var a: int = 0
if a:
echo "a is greater than 1"
Thank you!
I think that will work perfectly for my use case.
var fo: File
echo "Before open: Is fo 'unset'? ", fo.isNil
fo = open("./nim.org")
echo "After open: Is fo 'unset'? ", fo.isNil
fo.close
echo "After close: Is fo 'unset'? ", fo.isNil
fo = nil
echo "After explicitly setting to nil: Is fo 'unset'? ", fo.isNil
So far (from my brief experience with Nim), I found the file scanning method in Nim pretty concise.. I just need to do:
for line in lines("some_file.txt"):
# do things
I don't need to:
Nim does however support converter which allows you to do things like this:
converter toBool(x: int): bool = x > 1
var a: int = 0
if a:
echo "a is greater than 1"