TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Polymorphic Types in C [pdf]

99 pointsby sigsev_251over 1 year ago

13 comments

lernoover 1 year ago
The `any*` fat pointer in C3 is a version of this: <a href="https:&#x2F;&#x2F;c3-lang.org&#x2F;anyinterfaces&#x2F;" rel="nofollow">https:&#x2F;&#x2F;c3-lang.org&#x2F;anyinterfaces&#x2F;</a> Once this is available the step to embedding some runtime type information isn&#x27;t far.
评论 #39053354 未加载
评论 #39048857 未加载
评论 #39042394 未加载
评论 #39040598 未加载
illegalmemoryover 1 year ago
I had worked on something similar around 11-12 years back ( no way close to perfect or of any use :) ) <a href="https:&#x2F;&#x2F;github.com&#x2F;nautical&#x2F;CBullet">https:&#x2F;&#x2F;github.com&#x2F;nautical&#x2F;CBullet</a><p>Basically you can do something like this in c :<p>var value;<p>value = data(100);<p>&#x2F;&#x2F; or<p>value = data(0, 2, 3, 4, 5, 6);
injulyover 1 year ago
Polymorphic types are certainly useful, and I wish C had a better way to go about it, but this proposal feels like an odd patch to C&#x27;s type system. Especially this part:<p>``` Using a run-time value of type _Type T in _Var(T) can be allowed in general (and is useful), but needs to be restricted to contexts where full type information is not required at compile-time. ```<p>Semantic rules that conditionally apply only in some contexts is a common tendency of the C++ standard that many C programmers often dislike.
评论 #39044879 未加载
评论 #39040578 未加载
vkazanovover 1 year ago
Wow, suggesting polymorphic types to WG14 is... brave!
epsover 1 year ago
As a (not so) minor point -<p>It&#x27;s worth keeping in mind that code aesthetics is an important aspect of C codebases. There&#x27;s a lot of C code that is exclusively lowercase, sans the macros. So introducing keywords like _Type and _Var will serve to hinder their adoption, because it&#x27;d make the code that much more &quot;ugly&quot;. Just like what happened with _Generic - a reasonable feature, bad keyword selection -&gt; barely any field use.
评论 #39040649 未加载
synergy20over 1 year ago
IMHO, the C committee should just copy a subset from C++, in addition to having new features for free, you also make sure c and c++ are 100% compatible.
评论 #39046598 未加载
pgenover 1 year ago
Great, but I hate these leading underscores.
评论 #39039783 未加载
评论 #39039833 未加载
评论 #39040050 未加载
评论 #39039929 未加载
评论 #39045501 未加载
Dweditover 1 year ago
You can still use virtual function calls in plain C, you just do things the same way that C++ does things internally. Your first member of the struct is a Vtable, and you need to assign that member when you create the object. Your first parameter for the virtual method calls is a &quot;this&quot; pointer.
评论 #39046919 未加载
评论 #39042289 未加载
评论 #39044435 未加载
评论 #39047014 未加载
kazinatorover 1 year ago
&gt; <i>The following for [sic] declaration [sic] are then all equivalent:</i><p><pre><code> int i; int *ip; typeof(i) *i; _Var(_Typeof(i)) *i; </code></pre> What? How are &quot;int i&quot; and &quot;int *ip&quot; equivalent?<p><i>_Var(_Typeof(i)) ident</i> strikes me as incredibly silly. The type value produced by _Typeof(i) should be directly usable as a declaration specifier, without requiring hoisting to a name.<p>FFS, GNU C has had a sane typeof working for years.<p>To bind a type to an identifier, we don&#x27;t need a _Type type at all. <i>typedef</i> does this!<p>I.e. not:<p><pre><code> _Type x = int; </code></pre> but<p><pre><code> typedef int x; </code></pre> Existing, decades-old <i>typedef</i> is how you bind an identifier to a compile-time type value. There is no need to do this via a _Type type specifer, which then forces us to move the type we want into the initializer. We use the <i>typeof</i> storage class specifier, which then lets us have the type we want as another specifier, and we don&#x27;t need an initializer.<p>This works in GNU C:<p><pre><code> int x; typedef typeof(x) tx; &#x2F;&#x2F; same as typedef int tx; </code></pre> The problem of declared, named containers to capture type is long solved.
smcameronover 1 year ago
Is it just me, or are disillusioned C++ refugees who&#x27;ve gone back to C now trying to wreck it a second time? Probably just me. Since the 80&#x27;s, C++ served as an excellent decoy to absorb craziness and keep C sane.
评论 #39042444 未加载
评论 #39041658 未加载
评论 #39047720 未加载
评论 #39052889 未加载
sfpotterover 1 year ago
What problem does this solve that isn’t superficial? I don’t see why it’s important to include.
评论 #39044506 未加载
评论 #39043321 未加载
muragekibichoover 1 year ago
I use C at my day job and our company works with ANSI C. I believe it&#x27;s C89. I was curious to find out - who&#x27;s using C23 in prod? Please let me know in the comments. I&#x27;m super curious!
评论 #39047457 未加载
pjmlpover 1 year ago
The answer to most post-C89 features could be just use C++, but yeah, changing human nature is a lost fight.
评论 #39039801 未加载
评论 #39040033 未加载
评论 #39039861 未加载
评论 #39040802 未加载
评论 #39041203 未加载
评论 #39041352 未加载
评论 #39042467 未加载
评论 #39041907 未加载
评论 #39042217 未加载