www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Interfacing to C: const or immutable?

reply "Mathias Laurenz Baumann" <anonym001 supradigital.org> writes:
http://www.digitalmars.com/d/2.0/interfaceToC.html still says there is no  
const/immutable in D:

    There are no const or volatile type modifiers in D. To declare a C  
function that uses those type modifiers, just drop those keywords from the  
declaration.

but that is obviously out-dated. What should I use now, when interfacing  
with C?


--Marenz
Aug 26 2010
parent Michel Fortin <michel.fortin michelf.com> writes:
On 2010-08-26 18:06:24 -0400, "Mathias Laurenz Baumann" 
<anonym001 supradigital.org> said:

 http://www.digitalmars.com/d/2.0/interfaceToC.html still says there is 
 no  const/immutable in D:
 
     There are no const or volatile type modifiers in D. To declare a C  
 function that uses those type modifiers, just drop those keywords from 
 the  declaration.
 
 but that is obviously out-dated. What should I use now, when 
 interfacing  with C?
Most of the time, you'll want to use const when C does. But take note that const in D is transitive, so if you have a const pointer to mutable data in C you can't express that in D. I'd say just drop const for those cases. As for immutable, there's no such thing in C. If a function is documented as requiring a pointer to data that will never change, then it might make some sense to make the argument immutable instead of const. Also, if a C function returns a pointer to data that you positively know will never change, you could make it immutable too. But be careful with immutable: using it carelessly might cause the compiler to make the wrong assumptions and cause bugs. When in doubt, use const. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Aug 26 2010