Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

ocaml - String blit fails with error: This expression has type string but an expression was expected of type bytes

While trying to compile the following expression :

String.blit (String.make tfs.len ' ') 0 tfs.txt 0 tfs.len;

the compiler complains with the following error :

Error: This expression has type string but an expression was expected of type bytes
and the variable 'tfs.txt' (of type string) is pointed at.

As suggested in some forum, I tried to set the variable OCAMLPARAM to the value: safe_string=0,_ but with no result

I'm using OCaml 4.11.1 under Ubuntu 20.01.01 LTS


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since OCaml 4.10.0, the distinction between immutable string and mutable bytes is a configure-time default that cannot be changed on a program-by-program basis.

If you are trying to compile some old legacy code, it might make sense to use a version of OCaml configured without this distinction.

Otherwise, it would be more future-proof to adapt your code to use bytes when in-place mutation of strings is needed (which is not clear from your code snippet).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...