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


Groups > comp.lang.c > #149519

Re: is there a way to ask a variable what it is?

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: is there a way to ask a variable what it is?
Date 2020-01-30 12:06 +0100
Organization A noiseless patient Spider
Message-ID <r0udbc$gjc$1@dont-email.me> (permalink)
References (1 earlier) <ar223fdb4s1n4tgks99ni798ftjuot4i12@4ax.com> <r0r38m$r04$2@dont-email.me> <r0rteo$1ep6$1@gioia.aioe.org> <r0rvgj$9bq$1@dont-email.me> <r0scuu$1v1t$1@gioia.aioe.org>

Show all headers | View raw


On 29/01/2020 17:47, Manfred wrote:
> On 1/29/2020 1:57 PM, David Brown wrote:
>> On 29/01/2020 13:22, Manfred wrote:
>>> On 1/29/2020 5:55 AM, T wrote:
>>>> On 2020-01-28 20:36, Robert Wessel wrote:
>>>>> On Tue, 28 Jan 2020 19:19:38 -0800, T <T@invalid.invalid> wrote:
>>>>>
>>>>>> Hi All,
>>>>>>
>>>>>> Trivia question:
>>>>>>
>>>>>> In C, after you declare a variable, is there a way
>>>>>> to turn around and ask it what that variable is?
>>>>>>
>>>>>> Some thing like Raku's
>>>>>>
>>>>>>       > my uint $u; say $u.^name
>>>>>>       Int
>>>>>>
>>>>>> only correct?
>>>>>
>>>>>
>>>>> As Ben mentioned, there's no such feature in C.  Reflection was
>>>>> considered for C++20, but was deferred until the next major revision.
>>>>>
>>>>
>>>> Dang!
>>>
>>> It may be worth noting (as Bart mentioned) that C has a static type
>>> system, so the type of each variable is always known. For this reason C
>>> does not need reflection.
>>> The type system of C++ is statically checked as well, however, being
>>> considerably richer than C, it features RTTI, and may have some use for
>>> reflection. No urge for it though, as it can be deduced from Robert's
>>> comment.
>>
>> Actually, there are many good uses for reflection even in a statically
>> typed language.  In particular, it allows more generic coding.  A clear
>> example would be a JSON encoder/decoder - with reflection, you could
>> write templated functions that take a struct and do the encoding or
>> decoding, without needing the details of the type.
> At first thought it looks like encoding is a possibility, but decoding
> would substantially require the struct to be assembled at runtime, i.e.
> dynamic type support, I believe.

If you are thinking of decoding a JSON message and producing a matching
struct, then yes, that would need dynamic typing of some sort.  But it
is common to have incoming JSON messages where you know the structure
(or can quickly identify it) and you want to coerce it into an existing
struct type.  That can be done without dynamic typing.

> 
>   Another common
>> example would be matching database tables to type structures.
> Also requiring dynamic support as above.

Matching structs to existing tables requires dynamic typing - matching
tables to existing structs does not.

> 
> Now, a runtime supporting dynamic type checking and reflection can be
> implemented in a statically typed language (in fact I believe that Java,
> C#, Perl and friends do implement it in C - or C++) so it may as well
> end up in the standard library at some point in some form.

Yes.  It is questionable how useful it would be (compared to the
alternative of using hashmaps, vectors, variants, etc.) in a language
like C++.

> 
> When I wrote "some use" I was also thinking of e.g. template code that
> may enumerate members of a template argument (your JSON encoder).
> However a static type system cannot anticipate type information that is
> not available until run time (e.g. in a JSON stream), hence my tepid
> attitude.

Often you know exactly what should be in the incoming data - or at
least, you know about the parts that are interesting to you.  Your
parser would have to deal with missing data and extra data, of course.

> 
>>
>> The two main reasons reflection haven't made it into C++ yet are not
>> about lack of interest, lack of use-cases, or lack of urgency in getting
>> it into the language.  The first reason is that it is hard to design the
>> feature - there are just so many complications and corner cases.  The
>> second is that the next big step is to combine it with code generation,
>> and even if that takes another few years to appear, it is important that
>> reflection is made in a way that will work with the code generation
>> later.
> 
> My thought is that it is going to require substantial runtime support
> anyway, and the similarity with GC comes to mind.
> 
>>
>> But this is mainly a C++ idea - C does not have the same kind of generic
>> coding support.
>>
> 

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


Thread

is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 19:19 -0800
  Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 03:47 +0000
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:20 -0800
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-29 19:18 +0000
        Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 20:58 +0000
          Re: is there a way to ask a variable what it is? nobody@example.org (Scott) - 2020-01-30 15:59 +0000
            Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-30 16:35 +0000
    Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:55 -0800
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:24 +0000
        solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
          Re: solved: Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-30 03:16 -0800
  Re: is there a way to ask a variable what it is? Ian Collins <ian-news@hotmail.com> - 2020-01-29 17:28 +1300
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
      Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 06:49 +0000
      Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 08:08 +0100
      Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-30 00:14 +0000
        solved: Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 21:51 -0800
        Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-18 08:56 -0800
          Re: is there a way to ask a variable what it is? already5chosen@yahoo.com - 2020-02-19 01:19 -0800
            Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 02:53 -0800
            Re: is there a way to ask a variable what it is? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-02-19 12:45 +0000
            Re: is there a way to ask a variable what it is? James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-02-19 07:55 -0500
            Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-02-19 13:02 +0000
              Re: is there a way to ask a variable what it is? chad <cdalten@gmail.com> - 2020-02-19 07:27 -0800
            Re: is there a way to ask a variable what it is? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-22 09:42 -0800
  Re: is there a way to ask a variable what it is? Robert Wessel <robertwessel2@yahoo.com> - 2020-01-28 22:36 -0600
    Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-28 20:55 -0800
      Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 13:22 +0100
        Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 13:57 +0100
          Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:47 +0100
            Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-30 12:06 +0100
    Re: is there a way to ask a variable what it is? scott@slp53.sl.home (Scott Lurndal) - 2020-01-29 15:10 +0000
      Re: is there a way to ask a variable what it is? Manfred <noname@add.invalid> - 2020-01-29 17:07 +0100
  Re: is there a way to ask a variable what it is? Siri Cruise <chine.bleu@yahoo.com> - 2020-01-28 21:52 -0800
  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:11 +0000
    Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 13:30 +0100
      Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 12:44 +0000
        Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 14:03 +0100
          Re: is there a way to ask a variable what it is? gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-29 14:03 +0000
          Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 14:30 +0000
            Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 15:45 +0100
              Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 16:28 +0100
                Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:35 -0800
                Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 17:34 +0000
                Re: is there a way to ask a variable what it is? Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-29 18:48 +0100
    Re: is there a way to ask a variable what it is? Thiago Adams <thiago.adams@gmail.com> - 2020-01-29 04:43 -0800
      Re: is there a way to ask a variable what it is? David Brown <david.brown@hesbynett.no> - 2020-01-29 14:46 +0100
        Re: is there a way to ask a variable what it is? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-29 15:15 +0000
          Re: is there a way to ask a variable what it is? fir <profesor.fir@gmail.com> - 2020-01-29 08:11 -0800
          Re: is there a way to ask a variable what it is? T <T@invalid.invalid> - 2020-01-29 08:38 -0800
  Re: is there a way to ask a variable what it is? Bart <bc@freeuk.com> - 2020-01-29 11:23 +0000
  Re: is there a way to ask a variable what it is? John Bode <jfbode1029@gmail.com> - 2020-01-29 11:55 -0800
  Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100
  Re: is there a way to ask a variable what it is? jacobnavia <jacob@jacob.remcomp.fr> - 2020-02-19 12:23 +0100

csiph-web