Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #150490

Re: The language between C and C++ I would like to have

Newsgroups comp.lang.c
Date 2020-03-11 13:02 -0700
References <5a0784c9-ea64-409e-afd8-4aa4eb0cfd01@googlegroups.com> <9p8aG.709959$7i5.348395@fx35.am4>
Message-ID <a76b8df7-2ce0-499a-ac3d-e6cf65e29668@googlegroups.com> (permalink)
Subject Re: The language between C and C++ I would like to have
From Thiago Adams <thiago.adams@gmail.com>

Show all headers | View raw


On Wednesday, March 11, 2020 at 1:33:19 PM UTC-3, Bart wrote:
> On 11/03/2020 14:43, Thiago Adams wrote:
> > All these features are my suggestion for the C language.
> > 
> > 
> > 
> > # Member initializer
> > 
> > struct X
> > {
> >     int i = 1;
> >     struct Point pt = { .x = 1, .y = 1 };
> > };
> > 
> 
> I think I've made some objections before. But, if have a destructor, 
> then why not have a constructor where you can put this initialisation stuff?
> 
> (I can see one problem with that, as a constructor is a runtime 
> operation, and you may want this initialisation with static objects.)
> 
> Also, suppose you create a duplicate struct Y but with pt initialised to 
> (2,2) instead of (1,1), and then form a union of X and Y structs? (This 
> would be helped I think with my constructor idea.)
> 
> > # Overriding new
> > 
> > struct X
> > {
> >      char * name = NULL;
> > };
> > 
> > struct X* operator new(struct X* def)
> 
> Maybe 'new' can be the constructor I mentioned except that new() here is 
> called explicitly rather than implicitly.

Initially I was using init. It still an option. 
init brings a lot of more details. 

The default init for C could use return codes for runtime errors
to avoid exceptions. The final design using 'constructor/init' would be
very similar of C++ removing exceptions. Then new operator would have 
to call init as well and more...just like C++.
I don't have complains about this part of C++. But I am avoiding
fancy initialization in my code (in C++ and C).
In practice that means that my constructors just put the object at
an empty state that can be destroyed. The initialization that can 
fail is done is another function.

for instance, conceptually, instead of:

File f("file.txt")

I prefer:

file f = {};
open(f, "file.txt");

so at this moment init would not simplify my C code because the part that
may fail I put in another function like start/open/init .

The empty initialization would remove 1 macro per type in my code.
The new operator for compound literal would remove 1 function per
type that is allocated on the heap.
and destroy would remove many lines from implementation 
and one line (call) for each auto variable.

> > # Polimorphism
> > 
> > Pointers that can point to a set of types.
> > Types must have a common discriminant.
> > 
> > Sample:
> > 
> >   
> > //forward declaration
> > struct <struct Box | struct Circle> Shape;
> >   
> > //pShape points to box or circle
> > struct Shape* pShape;
> >   
> > struct Box
> > {
> >      int id = 1; //discriminant
> > };
> 
> 
> Perhaps you want a Tagged Union here (a 'sum type') where the 
> discriminant is an implicit field that is run-time checked.

With tagged unions the tag is not intrusive and if the size of objects
are very different the allocation is not intelligent.
Having two containers pointing to the same object (lest say a tree and map)
would put one tag per instance of each item in each container.

The intrusive model (where the tag is part of the object) we have just
pointers in each container and the objects can have different sizes.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-11 07:43 -0700
  Re: The language between C and C++ I would like to have chad <cdalten@gmail.com> - 2020-03-11 09:20 -0700
    Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-11 09:34 -0700
  Re: The language between C and C++ I would like to have Bart <bc@freeuk.com> - 2020-03-11 16:33 +0000
    Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-11 13:02 -0700
  Re: The language between C and C++ I would like to have Ian Collins <ian-news@hotmail.com> - 2020-03-12 08:20 +1300
    Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-11 12:45 -0700
      Re: The language between C and C++ I would like to have Ian Collins <ian-news@hotmail.com> - 2020-03-12 09:02 +1300
        Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-11 13:07 -0700
          Re: The language between C and C++ I would like to have scott@slp53.sl.home (Scott Lurndal) - 2020-03-11 21:13 +0000
            Re: The language between C and C++ I would like to have already5chosen@yahoo.com - 2020-03-11 14:47 -0700
              Re: The language between C and C++ I would like to have David Brown <david.brown@hesbynett.no> - 2020-03-11 22:55 +0100
              Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-12 05:23 -0700
          Re: The language between C and C++ I would like to have Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-11 22:29 +0000
  Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-12 05:40 -0700
    Re: The language between C and C++ I would like to have chad <cdalten@gmail.com> - 2020-03-12 13:06 -0700
  Re: The language between C and C++ I would like to have Philipp Klaus Krause <pkk@spth.de> - 2020-03-22 10:35 +0100
    Re: The language between C and C++ I would like to have Thiago Adams <thiago.adams@gmail.com> - 2020-03-23 04:57 -0700

csiph-web