Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
witch_t *navi
witch_t *navi
@navi@social.vlhl.dev  ·  activity timestamp 2 weeks ago

thinking of the namespace proposal for c, and, i wonder if, we couldn’t have namespaces done in the preprocessor, like:

/* lib.h */
#namespace lib lib_

void lib::foo(); /* declares lib_foo as a function name */

/* app.c */
#include <lib.h>
#using lib::foo
#using lib::foo as nya
#using lib as MyApi

int main(void) {
/* all of those would be pre-processed to lib_foo() */
lib::foo();
foo();
nya();
MyApi::foo();
}

works with macros, and identifiers, basically anywhere a #define would also work

and could be backwards compatible, e.g. if i were to use it in openrc:

/* rc.h */
#ifdef _HAS_NAMESPACE
#namespace rc rc_
#namespace RC RC_
#endif

now older c programs can still use rc_service_resolve, and newer ones can use rc::service_resolve (as well as #using directives)

including whole namespace, like #using lib, could work but it’d be a pain to implement i think, since the preprocessor would need to keep track of all lib_ it sees since #namespace was declared, and forward-replace them

but with or without whole-namespace-inclusion, this has really simple semantics and imo predictable errors, as namespacing conflicts can be reported the same way “redefinition” of macro names are

Akkomavalhalla

Akkomavalhalla

Akkomavalhalla

Akkomavalhalla

Akkomavalhalla

  • Copy link
  • Flag this post
  • Block
d@nny disc@ mc²
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp 2 weeks ago

@navi ppl will probably be negative about uses of the preprocessor bc they love to do that but i really like this application and it mirrors how i've been considering a way to produce versioned symbols (although i think solving that will probably mean actual compiler/linker support)

  • Copy link
  • Flag this comment
  • Block
d@nny disc@ mc²
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp 2 weeks ago

@navi i also like the idea of being able to provide a wrapper namespace or override the namespace name from the preprocessor command line although i'm not sure those would be necessary for the initial proposal of this functionality

  • Copy link
  • Flag this comment
  • Block
d@nny disc@ mc²
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp 2 weeks ago

@navi wow just saw the #using part. now that's cool. i really like this

  • Copy link
  • Flag this comment
  • Block
d@nny disc@ mc²
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp 2 weeks ago

@navi using whole namespace (::* imports in rust) is often considered a bad practice (in scala it's ._ imports and was considered bad there too) so if it's harder to support in this model i wouldn't consider that to be too much of a problem

  • Copy link
  • Flag this comment
  • Block
d@nny disc@ mc²
d@nny disc@ mc²
@hipsterelectron@circumstances.run replied  ·  activity timestamp 2 weeks ago

@navi cannot overstate how much i like this idea and want to support it in my build tools

  • Copy link
  • Flag this comment
  • Block

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.2-alpha.7 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Members
  • Code of Conduct