How to have constant reference/alias or pointer as it's parameter analogue on either
foo( int const * aPointer, const int & aliasVariable) { // ...
C++ parameter syntax
In default, parameters are immutable in Nim. Unlike C/C++, Nim doesn't have const type qualifier. Mutability is an attribute of symbol, not type.
Please read the Note in end of this section. https://nim-lang.github.io/Nim/manual.html#procedures-var-parameters
If you want immutable ref type: https://nim-lang.org/docs/manual_experimental.html#strict-funcs
If you want to define pointer parameters to declare C or C++ functions, you can use ptr int type but Nim doesn't have const pointer type.
var foo {.codegenDecl: "const $1 $2".}: ptr cint
#const int * foo
type ConstInt = const int #error
But we can import it. When translated into C, Nim int is defined as NI, so we can import const NI.
type ConstInt {.importc: "const NI".} = int
After that, ptr ConstInt in Nim will become const NI* in C.
In C++, alias type of Nim int is NI&, so we can import const NI&.
type ConstIntAlias {.importc: "const NI&".} = int
And then foo(int const * aPointer, const int & aliasVariable) can be defined as
proc foo(aPointer: ptr ConstInt, aliasVariable: ConstIntAlias) =
discard
I just know that Nim can't be as C++ way
so do we think the alias one would work, how'd it actually happen runtime error ?
type
ConstInt {.importc: "const NI".} = int
ConstCInt {.importc: "const int".} = cint
They can be used for parameters or pointers.
var p: ptr ConstCInt
#is translated to:
#const int * p;
But not variables.
var x: ConstCInt = 1 #invalid
#is translated to:
#const int x;
#x = 1;
Alias can also be imported from C++, and used for parameters.
type
IntAlias {.importcpp: "NI&".} = int
CIntAlias {.importcpp: "int&".} = cint
But it's meaningless since parameters are immutable.
proc foo(x: IntAlias) =
x += 1 #error
We cannot define alias variables.
var
x: cint = 1
y: CIntAlias = x #invalid
#is translated to:
#int x;
#int& y;
#y = x;
The only usage seem to be interfacing with C or C++.