Thread overview
[Issue 5251] New: Const C file
Nov 22, 2010
nfxjfg@gmail.com
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251

           Summary: Const C file
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-11-21 16:31:32 PST ---
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)*

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@yahoo.com
         Resolution|                            |INVALID


--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-11-22 08:58:52 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251



--- Comment #2 from bearophile_hugs@eml.cc 2010-11-22 09:34:42 PST ---
(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'?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251



--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-11-22 10:11:55 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251



--- Comment #4 from bearophile_hugs@eml.cc 2010-11-22 10:16:12 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5251


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg@gmail.com


--- Comment #5 from nfxjfg@gmail.com 2010-11-22 10:30:04 PST ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------