` git checkout devel # your version of devel git reset --hard nim-lang/devel #
reset to nim-lang/devel git push --force # push to github `
Run
There is some problem with my nim.cfg, I replace the nim.cfg to the original
one, it is fine now. I will check my nim.cfg to see what is the culprit.
When I run `nimble install jester`, it gives following error message:
Downloading https://github.com/dom96/jester using git
Tip: 3 messages have been suppressed, use --verbose to show them.
Error: Could not read package info file in
D:\Tmp\Temp\nimble_\githubc
for i in 0..5:
closureScope:
let x = i
test.add(
proc () = echo x
)
test[3]()
`import sugar var test: seq[proc ()] for i in 0..5: capture i: test.add( proc
() = echo i ) test[3]() `
Run
Thank you, I will try.
import "../../compiler" / [ast, vmdef, vm, nimeval, llstream]
import std / [os]
when isMainModule:
const moduleName = "script.nim"
let std = findNimStdLibCompileTime()
let intr = createInterpreter(moduleName, [std, std / "pure", std /
"core"])
proc
Good to hear the news. @Araq
Global variable always deepCopy, local variable `let` assign using shallowCopy
for value type, it is by design, not bug, not feature.
Add `mut` will pass the pointer of pointer.
proc p2(x: mut Node) =
x.left = nil # invalid
x = Node()# valid
proc p3(x: var mut Node) =
x.left = nil # valid
x = Node()# valid
Run
I try use the `var` `let` and `mut` to test some scenario. First, we define the
meaning of these terms(all for ref T type).
1\. `let` means fixed readonly variable binding, parameter without `var`
means `let` binding
proc p1() =
let x = Node()
x.left = nil # i
I think the `mut` proposal is more concise, look at the following code:
proc copy(n1: Node, n2: Node) {.write [n1.left, n1.right].} =
n1.left = n2.left
n1.right = n2.right
Run
Using the write effect has the following disadvantage:
1. It is verbose
Totally agreed, constructors must be treated differently from other proc to
construct immutable object.
Try add the following code:
template routes(body: untyped): untyped {.dirty.}=
block routes:
body
Run
Now let's talk about what the `let`, `var`, `=` means semantically for ref
type, when it is clear, then the programmer know what he is doing exactly, and
the compiler know what the programmer doing wrong. (We talk about the parameter
and return value later)
For value object type, they are clear
About `mut` or `var` syntax.
1\. For `T` is value type, `var x: T` or `x: var T`, here `x=y` or `x.p=y`
means change the `value` of `x`, so variable `x` is same as the `value`.
2\. For `T` is ref type, `var x: T` or `x: var T`, here `x` is the pointer to
the `value`, `x=y` means change the poin
`Immutability` just like `static type`, `noSideEffect`, it is very useful
feature to write reliable correct code, and we have no other way to make the
ref type immutable.
We can discuss the design first, and the plan according the implementation cost.
`T` is ref type.
1\. For any parameter `m` is not `mut T`
> 1. `let x = m` is fine
> 2. `var x = m` is disallowed
> 3. `var y; y.x = m` is disallowed
>
2\. For proc return result
> 1. If `proc f(...): va
Deep immutability is great feature!
`var T` is seems more consistent, but `mut T` is fine.
Good. :)
[https://scripter.co/notes/nim](https://scripter.co/notes/nim)/
It's great, is it possible add link to the official tutorial?
Copy the config directory too.
But I think maybe we can specify calling convention, so the following code may
correct:
asm """
mov eax, `a`
"""
Run
Otherwise, we cannot refer the parameter in the assembler code. But it still
make sense, because the vcc compiler treat the __declspec(naked
If the `asmNoStackFrame` disable any call convention pragma, a warning is
required when it is combined with call convention pragma or the code refer the
parameter name.
Add the pragma `.cdecl.` or `.stdcall.` to addInt doesn't change the type nim
compiler treat the parameter of addInt, maybe this is the bug.
Look at the assembler code:
@addInt_WYbpHhMOLfd0wzp13vORMw@8 PROC NEAR
mov eax, dword ptr [ebp-4H] ; 0020 _ 8B. 45, FC
add eax, dword ptr [ebp-8H] ; 0023 _ 03. 45, F8
ret
` type object3d*=object `
Run
No, the `=` function code is generated as __fastcall, and `&` function call `=`
as __fastcall.
If the `&` is not inlined in function `main` with compiler option `/Ob1` or
`/Ob0`, then all is fine. But when the function `&` is inlined in function
`main`, the code is generated as __stdcall to cal
I don't think so.
1. With x64, the call convention is all same.
2. The __fastcall is fast than __cdecl indeed.
3. The lastest compiler from vs 2013 has fixed this bug.
4. This is only happened with vcc.
5. It happened rarely.
Yesterday, I am testing nim destructor thing, and find a strange bug.
Look at the code modified from tests/destrcutor/tcustomstrings.nim
type
mystring = object
len, cap: int
data: ptr UncheckedArray[char]
var
allocCount, deallocCount: int
You can use fieldPairs to get field value by name.
type
Foo = object
field: int
proc `[]`(x: Foo, s: string, T: type): T =
for n, v in x.fieldPairs:
if n == s:
result = v
break
var foo = Foo(field: 123)
ec
I had tried this way, but failed. I can try again.
Yes, it can!!
I had tried this way:
cast[proc (x: string) {.nimcall.}](p2)
Run
It doesn't work.
The following code works:
import typeinfo
type
Foo = object
field: int
var foo = Foo(field: 123)
echo foo.field
let x = foo.toAny
echo x["field"].getInt
Run
The following post have detailed explanation of Go interface.
[https://forum.nim-lang.org/t/2422](https://forum.nim-lang.org/t/2422)
Current implementation doesn't support generic, because the vtable must be
build at compile time when impl is called, but with generic, this is not
possible, maybe we need the compiler to do it. :)
Here is the source:
[https://github.com/slangmgh/interfaced](https://github.com/slangmgh/interfaced)
I have created a modified version of Kru02's interfaced. The original post is
here: [https://forum.nim-lang.org/t/2422](https://forum.nim-lang.org/t/2422)
This version include more feature:
1. interface can inherited from other interfaces
2. interface to base interface can be converted impli
Find a way, the following code seems fine.
proc p2(x: int) =
echo x
proc p2(x: string) =
echo x
let t1: proc(x: int) {.nimcall.} = p2
let t2: proc(x: string) {.nimcall.} = p2
t1(10)
t2("string")
echo toHex(cast[int](t1))
e
When two proc have the same name, how can I get the proc address?
proc p2(x: int) =
echo x
proc p2(x: string) =
echo x
let t1 = p2 #
Run
I need a typed symbol.
Thank you. :)
The macro will executed in scope which define the type symbol, so the when
compiles will be always true.
I am writing a macro which need to know if a type symbol is exported or not.
But the getImpl doesn't contain export flag. The following code:
import macros
type
T1* = object
macro m1(t: typed): untyped =
echo t.getImpl.treeRepr
m1(T1)
Seems break with the config/nim.cfg, when I replace the nim.cfg with the new
devel version, the nim.exe execute fine.
I cannot compile the latest devel v0.20 successfully, never happened before. I
use vc2013 in window7.
Here is the output of command koch boot -d:release:
D:\Tool\Nim>koch boot -d:release
iteration: 1
compiler\nim0.exe c -d:release --nimcache:nimcache/r_windows_i386
compile
Great!!!
To make the text display correctly, you need to set the encoding of the
string(byte array) and the terminal codepage correctly.
If you encoding of the string(Depending you source file or the source of the
string) is unicode or utf8(\u5f9e is unicode), you need to set the terminal
codepage to 65
Here is the code from asyncpg package in apg_core.nim, it works before and
doesn't now:
# cast[](addr [0])
proc castPointer0(n: NimNode, v: string): NimNode {.compileTime.} =
result = newNimNode(nnkCast).add(
newIdentNode(v),
newNimNode(nnkCommand).add(
It just parameter, useless after call.
I need to get the pointer, then set the pointer to a struct. So I cannot just
use FFI.
@c0ntribut0r
Thank you, this is the method I use now.
Because I need to call c function, so I need the address of the string.
The code following is fine in the prevous version, but it failed with the
current devel branch because access the terminator zero is disabled.
var a = ""
let b = addr a[0]
Now, there is any way I can get the address of the empty string?
>From the previous threads, I think there is two important thing to do:
* Maintain the `nim compiler and core library` and keep them progress, which
make the nim appeal to people who like a wonderful program language.
* The `big standard package` which make most of user can develop with nim
Yes, I know that async is single thread. But the tinyc do work with thread.
Yes, it works for thread, async.
TinyC on Windows works well.
The TinyC compile speed and code optimization is not as good as vc, but it is
so small, the total size of tcc compiler and header/lib file is no more than 2M.
It can compile the nim compiler successfully!
The _cas code in `atomic.nim` for `tcc` of windows is disabled.
elif defined(tcc) and not defined(windows):
when defined(amd64):
{.emit:"""
static int __tcc_cas(int *ptr, int oldVal, int newVal)
{
unsigned char ret;
__asm__ __volatile__ (
Just grab the zip and extract them to some directory.
[https://nim-lang.org/download/nim-0.18.0_x64.zip](https://nim-lang.org/download/nim-0.18.0_x64.zip)
@GULPF
I need to define the variable first, then initialize the object later according
to different condition. And when other object reference this object with
`DateTime`, they need to be initialized when variable define if we don't want
to see the warning.
In nim, variable can be defiend as:
When I include the `DateTime` into the object, I cannot use the object freely
because the compiler always complain that the `DateTime` field is not
initialized.
type
User = object
name: string
age: int
birthday: DateTime
var u = User(nam
I like the idea to make `seq` and `string` default `@[]` and `""`, we can use
`{.noinit.}` to make them `nil` if care about performance.
@Araq
Yes, I think static[T] should be nnkLitXXX, and typedesc should be nnkLitType
maybe. Every should be NimNode in the macro, but it affect compatibility.
typedesc seems different from other type in the macro.
macro m1(T: typed): untyped =
echo treeRepr T
macro m2(T: typedesc): untyped =
#echo treeRepr T # compile error
echo repr T.getType
m1(int) # Sym "int"
m2(int) # typeDesc[int]
Change the code:
template create(x: int, T: typedesc): untyped = T(age: x)
to:
template create(x: int, T: untyped): untyped = T(age: x)
All the code of above works!
Now here is problem how to pass typedesc to template:
template create(x: int, T: typedesc): untyped = T(age: x)
macro m2(T: typedesc): untyped =
let nT = quote do: `T` #Sym "User"
let t1 = T.getType()[1] #Sym "User"
let t2 = bindSym"Us
The following code is ok:
result = newCall("stepTwo", ident("User")) # OK!
result = newCall("stepTwo", T.getType) # OK!
Now, I know how to pass User as typedesc, but still it is a little confusion
about typedesc to me.
When a proc call a macro with typedesc parameter, we can just user the type
name, but when we call another macro in the macro and pass the typedesc
parameter to it, something is strange, look at the code:
type
User = object
id: int
name: string
a
@dom96 is right, it can be done like this:
macro withTran(db, body: untyped): untyped =
quote do:
block:
proc action(`db`: apgPoolConnection) {.async.} =
`body`
let fut = doTransaction(action)
yield fut
I think maybe this is template bug. These code doesn't compile from @zielmicha.
template bar() {.dirty.} =
echo a
template foo() =
let a = 5
bar()
foo()
But when we turn foo into proc, the code works.
template bar() {.dirt
The following code is extracted from asyncpg.
template withConnection*(pool: apgPool, conn, body: untyped) =
## Retrieves first available connection from pool, assign it
## to variable with name `conn`. You can use this connection
## inside withConnection code block
The document is about exception raised by async proc, my problem is about the
exception raised by called proc between async code using resource, they are
something different. Maybe I can wrapper the body with a proc which turn
exception into return value.
@dom96 Thanks. The code seems works, but it doesn't. db.close() seems executed
before the code 'block', this is the error message:
asyncdispatch.nim(1492) waitFor
asyncdispatch.nim(1496) poll
asyncdispatch.nim(292) runOnce
Error: unhandled exception: No handles or tim
We can create template with like this:
template withFile(name: string, body: untyped) =
let fh = open(name, ...)
defer: close(fh)
block:
body
But when template contain await, the compiler will complain the 'await' is
undeclared identifier. The co
It works finally. To make the {.async.} works in quoted code, the 'await' and
Future variable must be ident!!
type
Result* = ref object of RootObj
body*: string
proc waitEvent(evt: AsyncEvent): Future[void] =
var fut = newFuture[void]("callAsync")
It's seems not perfect, but works, thank you.
I think what I really need is a new spawn which return a Future[T] rather than
FlowVar[T].
Now there is another problem. I hope I can write the code like this:
proc ff1(p1: int): Result =
# my db acess code here
..
proc ff2(p1, p2: int): Result =
# my db acess code here
..
proc main() {.async.} =
# async call ff1
@adrianv Thank you
@2vg asyncpg is good, thank you!
Find a way to solve the problem. Here is the code:
proc doWork(evt: AsyncEvent) {.gcsafe.} =
sleep(3000)
evt.trigger()
echo "work over"
proc waitEvent(ev: AsyncEvent): Future[void] =
var fut = newFuture[void]("waitEvent")
proc cb(fd: AsyncF
I encounter a problem, and hope someone would help me. Here is the problem:
1. I use Jester to deal with socket reques/response, which is single
threaded(main thread).
2. I use postgresql to store/acess data, which is block function.
3. I think I can use postgresql in threadpool, then retur
@LeuGim
One little question. When I import the macros, the getType of this file is
used, not the macros's getType, this is strange. That's mean import macros
affect the template in this file can be used or not.
I found a way to deal with the problem finally. We cannot define an template
which return an 'type' (sometime it will fail to compile), but if I return a
variable of the type, it is ok, then I can get the type of the variable.
template get_val_from_item(seq1: untyped): untyped =
@mashingan Yes, I can convert the iterator to seq and all works. But it not the
way I want. I hope the iterator is same as seq, like following code:
print collectz(1..5, @[toSeq(1..it)])
print collectz(@[1,2,3], @[toSeq(1..it)])
print collectz(countup(1,4), @[toSeq(1..it)])
@cdome That doesn't work, because iterator is treated as untyped, and untyped
parameter cannot be overloaded. The following code cannot compile.
proc f1(t: seq[int]) = discard
template f1(t: untyped) = discard
f1(@[1])
f1(countup(1,3))
@cdome Thank you.
I am writing function like map, it will take the type array, slice, seq,
iterator, etc as parameter, because the iterator cannot be treated as typed, so
I cannot use typed(If the paramter type is typed, the function in the code can
compile successfully).
The code like this wo
I need get the type of container item(seq, array, iterator etc), so I write an
template to get the item type, but when work with the generic, sometime it will
fail to compile using the wrapper. Here is the code
import macros
template getTypeOfItem(t: untyped): auto =
The compiler show the following message:
ttype.nim(29, 4) template/generic instantiation from here
ttype.nim(16, 24) Error: type expected
When I browse the compiler source today, I find that the REPL is already here
and is PERFECT for me.
Thank you for clarification.
There is simple version to show the problem.
import typetraits
type Foo[T] = ref object
x: T
proc `@@`*[T](a: T): Foo[T] = new(result); result.x = a
proc `$`*[T](f: Foo[T]): string = "Foo[" & $f.x & "]"
type Bar[T] = Foo[Foo[T]]
# OOPS
A compiler problem when I using generic. the code:
import sequtils, typetraits
type
Point = object
x,y: int
rseq[T] = ref object
inq: ref seq[T]
Vector[T] = rseq[T]
proc P(x, y: int): Point = Point(x: x, y: y)
proc `+`(p1
Test more, find this code works:
echo concat([@[@[1,2]], @[@[3,4]]])
Turn the arguments into array, then the varargs parameter works.
This code is ok:
echo concat(@[1,2], @[3,4])
But this code doesn't compile:
echo concat(@[@[1,2]], @[@[3,4]])
94 matches
Mail list logo