let versus var with objects

2020-11-06 Thread xigoi
There _is_ a function defined for the type (the generic `$` that works for every object), it's just not the one you want.

let versus var with objects

2020-11-06 Thread HJarausch
Many thanks for the explanation. It's still confusing to me. I'm coming from C++, Python, ... Algol68. If a function or operator is applied to an object of type X, the function defined in/for that type X should be called or this should be flagged as an error if there is an ambiguity.

let versus var with objects

2020-11-06 Thread ynfle
This happens because there is a `$` proc defined for all objects which the compiler assumed that you wanted to use because the one you defined didn't match the type of the object that you wanted to give it. To get around this and explicitly try to use the proc in your module you can specify the

let versus var with objects

2020-11-06 Thread HJarausch
This looks like a bug to me. If there is a procedure which expects a `mutable` argument, but the user tries to call it with an `immutable` argument, the Nim compiler should flag this as an error. In a larger project it is very hard to find (user) bugs if the Nim compiler just creates a dummy ar

let versus var with objects

2020-11-05 Thread HJarausch
Many thanks, I still have much to learn, Helmut

let versus var with objects

2020-11-05 Thread jrfondren
That `var` is the problem. It requires mutable access to Mpfr, which a `let` Mpfr forbids. I guess you added that so that you could get `m.mpfr.addr`? Just remove the `var` and switch that to `m.mpfr.unsafeAddr`. `unsafeAddr` isn't as scary as it sounds apparently:

let versus var with objects

2020-11-05 Thread Araq
The only reason I can think of is that somebody wrote a broken `$` operator that requires a `var T` parameter.

let versus var with objects

2020-11-05 Thread HJarausch
So please, tell a newbie who hasn't written this proc what's wrong with it. Many thanks Helmut I have omitted the backward quotes since I don't know how to get it into RST > proc $(m: var Mpfr): string = > > var buf: cstring > > mpfr_asprintf(buf.addr, fmt"%.{ctx_Print_Prec}Rg", m.mpfr.addr) >

let versus var with objects

2020-11-05 Thread HJarausch
I have bindings to the mpfr library (from the net) which works just fine. E.g. proc sqr(m: Mpfr): Mpfr = result.init() discard mpfr_sqr(result.mpfr.unsafeAddr, m.mpfr.unsafeAddr, ctxRnd) var x= 3.Mpfr * * * This works just fine var xsqr = sqr(x) echo $sxqr BUT let xsqr = sqr(x) echo $