Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make choice between newtype and type configurable #308

Open
edsko opened this issue Nov 28, 2024 · 1 comment
Open

Make choice between newtype and type configurable #308

edsko opened this issue Nov 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@edsko
Copy link
Collaborator

edsko commented Nov 28, 2024

We can do this at two levels:

  • A global default: do we want to use Haskell newtypes or type aliases for C typedefs (which would then apply to typedef int my_clock_t as well as typedef struct foo foo_s equally). Currently the default here is to use newtypes everywhere (see also Is typedef a type or a newtype or? #173)
  • More targeted overrides of the defaults, when using hs-bindgen as a library (see below).

Background:

In #306 we discussed what to do with things like

struct foo { int x; int y; };
typedef struct foo foo_s;

The conclusion was that we'd want to generate a Haskell newtype for foo_s, unless foo_s and foo are spelled identically.
We could alternatively make foo_s a Haskell type alias for foo, but this feels somewhat inconsistent with #173; in particular, although it is hard to imagine that we might want different type class instances for foo and foo_s in this example, it's entirely conceivable we might have something like

typedef foo_s some_special_use_of_foo

elsewhere in the header file for which we do want to generate a newtype. One could imagine heuristics such as

  • if the name foo and foo_s are "sufficiently similar" (_s suffix, ..?)
  • if the foo and foo_s are "suffiently closely" defined (same C definition, consecutive lines, ..)

but unless we can find rules that seem to apply quite universally, it might be better to leave these to individual users (using hs-bindgen`-as-a-library); we should however provide the necessary hooks to implement such rules.

@edsko edsko added the enhancement New feature or request label Nov 28, 2024
@edsko edsko added this to the 1: `Storable` instances milestone Nov 28, 2024
@phadej
Copy link
Collaborator

phadej commented Dec 2, 2024

I realise that we need this for this example:

// in stdbool.h
#define bool _Bool

if we would treat #defines similarly as typedefs (currently we generate newtype for both), we'd generate (using current name mangling rules)

newtype CBool' = MkCBool' CBool

but in this particular case we really want type, or maybe not generate any Haskell representation (i.e. inline alias, a third option between newtype and type).

For now we can hardcode bool case, but I think the same machinery can be used for it as for "user" defined types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants