Deutsch English Français Italiano |
<20240424183818.814@kylheku.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: "not-const" qualifier for C Date: Thu, 25 Apr 2024 01:47:45 -0000 (UTC) Organization: A noiseless patient Spider Lines: 61 Message-ID: <20240424183818.814@kylheku.com> References: <v0bma4$2g9n9$1@dont-email.me> <20240424133951.155@kylheku.com> <29954376-ec3c-41ca-b1c7-27b4faab6625@gmail.com> Injection-Date: Thu, 25 Apr 2024 03:47:45 +0200 (CEST) Injection-Info: dont-email.me; posting-host="beacde28bb5b90842ad087fd24932ac8"; logging-data="2784043"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mw7FzhduDvd/DGhi8XhGDyjINGMCMmAQ=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:ILTacgnIVMWio49qluAOfhUNuC4= Bytes: 2864 On 2024-04-25, Thiago Adams <thiago.adams@gmail.com> wrote: > > > Em 4/24/2024 8:13 PM, Kaz Kylheku escreveu: >> On 2024-04-24, Thiago Adams <thiago.adams@gmail.com> wrote: >>> Motivation sample: >>> >>> struct X { >>> const char* const type; >>> }; >>> >>> struct X * make_x(){ >>> struct X * p = malloc(sizeof *p); >>> if (p) >>> { >>> >>> p->type = strdup("X"); // *** error, type is const *** >>> >>> if (p->type == NULL) >>> { >>> free(p); >>> p = NULL; >>> } >>> } >>> return p; //ok >>> } >> >> Different idea: allow all conversions without a cast which only >> add qualifiers anywhere in the type: >> >> struct X { >> const char* const type; >> }; >> >> struct mutable_X { >> char* type; >> }; > > In this case the types struct X and struct mutable_X are not > convertible. They are not the same type. And your mutable keyword is a syntax error in the current C language! The idea is that we allow "pointer to struct mutable_X" to convert to "pointer to struct X" without a cast, because the two types are structurally equivalent, and every element of the destination type at least as qualified as its counterpart in the source type. I don't entirely like the idea because it entails structural equivalence. That has various problems. Ideological: we are used to "struct foo" and "struct bar" (in the same translation unit) being different types. Structural equivalence requires complete types. We could end up with the situation where "foo *" cannot convert to "bar *" without a cast in one scope in the program where foo is an incomplete type, but in another scope, foo is completed in a way that the assignment is compatible. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca