You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
distinguish 2 kinds of null terminated strings, mapping to char* and const char *, eg:
type ccstring =ptrchar# maps to const char*, `c` stands for consttype cmstring =ptrchar# maps to char*, `m` stands for mutable
(currently, cmstring is the same as cstring on c backend but on js backend, ccstring would be a better fit to map js' string since js strings are immutable)
this will improve type safety in a number of places, in particular around FFI and ROM data (Example 1), and avoid subverting the type system (Example 2)
bikeshedding
better names welcome
implementation
this implementation works: nim-lang#3720 (comment)
and it's generic (not just for const char* but also const T* or even const T)
typeConstCharPtr*=ptrCppConst[char] # maps to const char*
example 1
would avoid this classic SIGBUS problem when writing to ROM: nim-lang#8463 (issue still unfixed)
var s1 ="abc".cstring# give a warning (ultimately an error)var s2 ="abc".cmstring # CT errorvar s3 ="abc".ccstring # ok
s3[0] ='x'# CT error
example 2
in example below, var s = s is needed so that s[0] = 'x' compiles, however it's a hack, we shouldn't be able to subvert the type system without using a cast;
either proc main(s: cstring) = s[0] = 'x'should compile without addingvar s = s`
or var s = s should be illegal (instead let s = s)
or s[0] = 'x' should be illegal
procmain(s: cstring) =var s = s
s[0] ='x'var s ="abc"var s2 = s.cstringmain(s2)
doAssert s =="xbc"
under this proposal, you'd have:
procmain(s: cmstring) =# var s = s # now not needed but would still work with it uncommented
s[0] ='x'var s ="abc"var s2 = s.cmstring
main(s2)
doAssert s =="xbc"
rationale
why introduce cmstring instead of keeping cstring?
to avoid breaking code and make code transisioning to cmstring, ccstring easier (forward and backward compatible)
eg, in js, string maps closest to ccstring, not cmstring, but cstring is already also mapping to js string
there are other reasons which i can explain.
TODO
specify what conversions, operations are legal for ccstring and cmstring
note
in D, there's 3 notions for mutability of runtime data: mutable(the default), const, and immutable (I'm not considering CT notions of enum and static).
The missing part in the nim picture would be immutable here.
The text was updated successfully, but these errors were encountered:
timotheecour
changed the title
RFC: distinguish ccstring (const char*) from cmstring (char*)
distinguish ccstring (const char*) from cmstring (char*)
Jan 13, 2021
proposal
distinguish 2 kinds of null terminated strings, mapping to
char*
andconst char *
, eg:(currently,
cmstring
is the same ascstring
on c backend but on js backend,ccstring
would be a better fit to map js'string
since js strings are immutable)this will improve type safety in a number of places, in particular around FFI and ROM data (Example 1), and avoid subverting the type system (Example 2)
bikeshedding
better names welcome
implementation
this implementation works: nim-lang#3720 (comment)
and it's generic (not just for
const char*
but alsoconst T*
or evenconst T
)example 1
would avoid this classic SIGBUS problem when writing to ROM:
nim-lang#8463 (issue still unfixed)
see nim-lang#8463 (comment)
under this proposal, we have:
example 2
in example below,
var s = s
is needed so thats[0] = 'x'
compiles, however it's a hack, we shouldn't be able to subvert the type system without using acast
;proc main(s: cstring) =
s[0] = 'x'should compile without adding
var s = s`var s = s
should be illegal (instead let s = s)s[0] = 'x'
should be illegalunder this proposal, you'd have:
rationale
to avoid breaking code and make code transisioning to cmstring, ccstring easier (forward and backward compatible)
eg, in js, string maps closest to ccstring, not cmstring, but cstring is already also mapping to js string
there are other reasons which i can explain.
TODO
specify what conversions, operations are legal for ccstring and cmstring
note
in D, there's 3 notions for mutability of runtime data: mutable(the default), const, and immutable (I'm not considering CT notions of enum and static).
The missing part in the nim picture would be immutable here.
The text was updated successfully, but these errors were encountered: