Thank you very much. Yes, I need to call this function from C.
For example, I need to add a callback function to the button of the UI system.
The UI system is a c library. I will first store the pointers to rawProc and
rawEnv and release them when they are not needed.
` import system proc testCallback(arg: int): proc() {.closure, gcsafe, locks:
0.} = result = proc() {.closure, gcsafe, locks: 0.} = echo "res", arg var p1 =
testCallback(5) var p1Pointer = system.rawProc(p1) # How to cast back
cast[proc() {.closure, gcsafe, locks: 0.}](p1Pointer)() `
Run
Obviously the following is wrong:
proc testCallback[T](callback: proc(this: T) {.closure, gcsafe, locks: 0.})
= callback()
type TestType = object
discard
proc helloNim(this: TestType) = echo "Hello, Nim!"
proc testProc(this: TestType) =
testCall
import system
proc testCallback(arg: int): proc() {.closure, gcsafe, locks: 0.} =
result = proc() {.closure, gcsafe, locks: 0.} = echo "res", arg
var p1 = testCallback(5)
var p1Pointer = system.rawProc(po1)
Run
?? How to cast back `cast[proc() {.
import system
proc testCallback(arg: int): proc() {.closure, gcsafe, locks: 0.} =
result = proc() {.closure, gcsafe, locks: 0.} = echo "res", arg
var p1 = testCallback(5) var p1Pointer = system.rawProc(po1)
?? How to cast back cast[proc() {.closure, gcsafe, locks: 0.}](p1Pointer)()
I don't understand why I can't cast proc(){.closure.} to pointer. but I can
cast proc(){.nimcall.} to pointer ?
Yes, I use multiple operating systems, I am fed up with the free-standing
procedure of the c language, but I have to do this (although I can also write
oop-like code, but it is a chicken rib in c). but in nim can reflect oop-like,
such as the existence of inheritance.
But for a lot of reasons,
type
T0 = object T1 = object T = object O = object
converter (this: any) toT (): T = discard proc (this: T0) p (): O = discard
proc (this: T0) p (that:T1): O = discard proc (this: T0) p (that:T1,
rest:varargs[T, toT]): O = discard proc (that :T1) p (this:T0): O = discard
Cann't nim be desig
I am translating my go code into nim, which looks a bit messy compared to go.
It's clearer to put the current class object before the name, **_proc (this:
MyType) named (arg1: int)._** Why are class objects and parameters written in
the same brackets in NIM?
nim:
...
#edi
type
MyType* = ref object
a*: int32
b*: int32
c*: int32
proc myProc*() = discard
Run
I don't understand why it is placed on the right side. Is there any reason for
this design? Feel good on the left side.
type
*MyType
in nim manual Type sections A type section begins with the type keyword. It
contains multiple type definitions. A type definition binds a type to a name.
Type definitions can be recursive or even mutually recursive. Mutually
recursive types are only possible within a single type section. Nominal
The premise of not using this way is that the type is perfect design from the
beginning , but not all type can be designed perfectly from the very beginning.
Not even with method
In c++
class Preant {
public:
void call() {
}
};
class Child : public Preant {
private:
void call();
};
Run
In nim
type
Preant* = ref object of RootObj
proc call*(this: Preant)
14 matches
Mail list logo