Comment #0 by bearophile_hugs — 2010-11-21T16:31:32Z
I think this program is supposed to work, because fprintf() and fclose() don't modify 'fout':
import std.c.stdio: fopen, fclose, fprintf;
void main() {
const fout = fopen("test.txt", "w");
fprintf(fout, "%d", 10); // ERR
fclose(fout); // ERR
}
But DMD 2.050 shows the errors:
test.d(4): Error: function core.stdc.stdio.fprintf (shared(_iobuf)* stream, in const(char*) format,...) is not callable using argument types (const(shared(const(_iobuf))*),string,int)
test.d(4): Error: cannot implicitly convert expression (fout) of type const(shared(const(_iobuf))*) to shared(_iobuf)*
test.d(5): Error: function core.stdc.stdio.fclose (shared(_iobuf)* stream) is not callable using argument types (const(shared(const(_iobuf))*))
test.d(5): Error: cannot implicitly convert expression (fout) of type const(shared(const(_iobuf))*) to shared(_iobuf)*
Comment #1 by schveiguy — 2010-11-22T08:58:52Z
(In reply to comment #0)
> I think this program is supposed to work, because fprintf() and fclose() don't
> modify 'fout':
Yes they do. fprintf and fclose deal with a memory-allocated buffer that will be used to optimize I/O throughput.
Comment #2 by bearophile_hugs — 2010-11-22T09:34:42Z
(In reply to comment #1)
> Yes they do. fprintf and fclose deal with a memory-allocated buffer that will
> be used to optimize I/O throughput.
If this bug report is invalid then thank you for closing it. I am not expert enough on this.
But I don't understand what you have said. Even if fprintf and fclose deal with a memory-allocated buffer, do they modify the value of 'fout'?
Comment #3 by schveiguy — 2010-11-22T10:11:55Z
const is transitive, so if you imagine a FILE having this structure:
struct FILE
{
int fd;
ubyte[] buffer;
}
Now, if FILE is const, then buffer is const(ubyte[]), so how does fprintf write to that buffer?
What you are looking for is head-const (the variable cannot change, but everything it points to can), which has some good uses, but does not exist in D.
Comment #4 by bearophile_hugs — 2010-11-22T10:16:12Z
(In reply to comment #3)
> const is transitive, so if you imagine a FILE having this structure:
>
> struct FILE
> {
> int fd;
> ubyte[] buffer;
> }
>
> Now, if FILE is const, then buffer is const(ubyte[]), so how does fprintf write
> to that buffer?
>
> What you are looking for is head-const (the variable cannot change, but
> everything it points to can), which has some good uses, but does not exist in
> D.
I understand, you are right, thank you.
Comment #5 by nfxjfg — 2010-11-22T10:30:04Z
(In reply to comment #3)
> What you are looking for is head-const (the variable cannot change, but
> everything it points to can), which has some good uses, but does not exist in
> D.
It exists to some degree in D1:
final int[] a = [1,2];
a[0] = 1; //works
a = [5,6]; //Error: cannot modify final variable 'a'
Apparently this was disabled in D2.