Re: How to chain prodecure calls that mutate a reference object ?

2020-01-19 Thread ducdetronquito
Hey !

Thanks you for the quick answers !

As @solo989, just returning var Child like in the snippets below does not work 
has it triggers a compile error.


proc set_value*(self: var Child, value: int): var Child {.discardable.} =
self.value = value
return self


proc get_child*(self: var Parent): var Child {.discardable.} =
var child = Child()
self.child = child
return child

# Raises:
# Error: 'child' escapes its stack frame; context: 'child'; see 
https://nim-lang.org/docs/var_t_return.html


Run

Finally, @mratsim solutions works fine and make perfect sens :) 


How to chain prodecure calls that mutate a reference object ?

2020-01-19 Thread ducdetronquito
Hi there !

I am trying to chain procedures that mutate a reference object, but I can't 
managed to do it.

Here is a minimal example:


type
Child* = ref object
value: int

Parent* = ref object
child: Child


proc set_value*(self: var Child, value: int): Child {.discardable.} =
self.value = value
return self


proc get_child*(self: var Parent): Child {.discardable.} =
var child = Child()
self.child = child
return child


var parent = Parent()

parent.get_child().set_value(10)



Run

This example triggers the following compile error, but I dont understand why I 
can't mutate the reference return by the get_child procedure.


Error: type mismatch: got 

but expected one of:
proc set_value(self: var Child; value: int): Child
  first type mismatch at position: 1
  required type for self: var Child
  but expression 'get_child(parent)' is immutable, not 'var'

expression: set_value(get_child(parent), 10)


Run

Any ideas ?

Thanks :)


Re: Check if a procedure exists for a given type at compile time

2020-01-12 Thread ducdetronquito
Wow, I didn't think about cyclic imports at first !

I ended up with the approach you mentioned: using a proc type field with a 
default value that can be overriden by the end user with a setter.

Finally, messing around with type introspection would have been more 
complicated to understand compared to this simple solution.


.
   # Define in the library
type
Callback = proc (self: Car)
Car = ref object
speed: int
drive_callback*: Callback
 
 
 proc new*(car_type: type[Car]) Car =
  return Car (drive_callback: default_drive)
 
 
 proc default_drive(self: Car):
  echo "vroom"

proc set_drive_callback*(self: Car, callback: Callback) =
self.drive_callback = callback
   
   
   # Define by the user
var car = Car()
car.set_drive_callback(
 proc (self: Car) =
 echo "VROOM !!"
)



Run

PS: Thanks @dawkot for the hint, I might use it for another thing !


Check if a procedure exists for a given type at compile time

2020-01-12 Thread ducdetronquito
Hello everyone,

I would like to know if Nim allows me to check if a procedure exists for a 
given type at compile time ?


.
# As a library author I define a type `Car`
type
Car = object
speed: int
 
 proc default_drive(self: Car):
  echo "vroom"
 
 
 # A user of this library define a procedure `drive` on this `Car` type
 proc drive(self: Car):
 echo "VROOM !!"

var car = Car()

# Somewhere in my library, I want to check if the procedure `drive` 
exists on type `Car`
# NB: The code below is pseudo code
if prodecure_exists(Car, "drive"):
car.drive()
else:
car.default_drive()


Run

Have a nice day :)


Re: How to parse RSS feeds with Nim

2019-12-23 Thread ducdetronquito
Simple and straight to the point article, good job :)

Ps: On your home page, the excerpt does not display nicely as it shows a 
portion of the RSS feed in the article.


Re: Advent of Nim 2019 megathread

2019-12-01 Thread ducdetronquito
Good catch ;)


Re: Advent of Nim 2019 megathread

2019-12-01 Thread ducdetronquito
Here we go :) 
[https://github.com/ducdetronquito/AdventOfCode2019](https://github.com/ducdetronquito/AdventOfCode2019)


Re: when to use 'ref object' vs plain 'object'

2019-11-21 Thread ducdetronquito
Dammit ! That error has bitten me this weekend and I wasn't able to understand 
its origin by reading the error message.

Does someone why plain objects do not work with async ?


Re: Import best practices

2019-10-20 Thread ducdetronquito
Thanks a lot, your article is really nicely written and straight to the point ! 
:)


Import best practices

2019-10-20 Thread ducdetronquito
Hi everyone,

Being new to Nim and coming from Python, I often find myself struggling with 
imports.

For example, I play with the asynchttpserver module of the standard library, 
and I don't know how to properly import it.

**Solution 1** :

Everything is imported, but is it OK to import/pollute the namespace with 
procedures/types that I don't use ? Does it makes binaries bigger at the end ? 


import asynchttpserver


Run

**Solution 2** :

Import only what's needed, but it is a bit verbose and you often forget to 
import operator procedure and the compiler is not happy about it.


from asynchttpserver import HttpCode

echo HttpCode(200) != HttpCode(404)



Run

By the way, how do you import operator procedures (like ==) ? Is it possible to 
import an operator procedure for a specific type ?


from asynchttpserver import `==`


Run

Does it import all == procedures ?

**Extra** :

Is there a way to import all procedures related to a specific type ? In python, 
when you import a class, you have access to all its methods / variables as they 
are defined within the class.

Have a nice sunday ️


Re: How to use testament ?

2019-10-13 Thread ducdetronquito
Ok, thanks for the clarification :)


Re: How to use testament ?

2019-10-13 Thread ducdetronquito
Thanks for the quick reply !

I will look into the code of the Nim compiler. But is there any other project I 
could look at ?

Even though I am currently toying with the language, I expect to build 
production grade application at some point, so I don't want to use already 
deprecated components. 


How to use testament ?

2019-10-13 Thread ducdetronquito
Hi everyone !

I was looking to add unit tests to my toy project, and the 
[documentation](https://nim-lang.org/docs/unittest.html) leads to a tool called 
testament.

Unfortunately, there is no documentation about it 
[yet](https://github.com/nim-lang/Nim/issues/12251).

Until it comes, do you know some Nim projects that have unit tests working with 
testament so I can try to understand how it works ?

Thanks in advance and enjoy your sunday ☕


Re: Define a procedure on a type itself ?

2019-10-11 Thread ducdetronquito
Thanks! I am so glad it exists :)


Define a procedure on a type itself ?

2019-10-11 Thread ducdetronquito
Hi everyone,

I am wondering if it is possible to define a procedure on a type itself ? 
Coming from the Python world, it would be called a _class method_ or a _static 
method_.

I really like to use this kind of syntax to create objects, and in Nim I 
imagine it could work like:


type
User = object
name: string


proc create_admin(user_type: Type[User]): User =
return User(name: "admin")


let dobby = User.create_admin()


Run


Re: Nim for Beginners Video Series

2019-10-08 Thread ducdetronquito
Hello @Kiloneie !

First really nice job, keep going there is not much of these Nim tutorials out 
there !

My only advice would be to make your voice more clear. Right now, your voice is 
a bit low and the sound of you hitting your keys is a bit loud. Maybe you could 
first record the video and then your voice ?

Have a nice day !


Re: Trying to make dict in dict (table in table)

2019-10-08 Thread ducdetronquito
Hi @vbxx3 !

What is the type of httpMethod in the addRoute* method ? You haven't defined it 
in the method definition.


Re: Passing procedure as argument

2019-10-06 Thread ducdetronquito
Hello vbxx3,

I am new to Nim but I think what you are looking for is called **procedural 
types**.

You can find some information about them in the [Nim 
tutorial](https://nim-lang.org/docs/tut1.html#advanced-types-procedural-type) 
and in the [manual](https://nim-lang.org/docs/manual.html#types-procedural-type)