nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

`export` as pragma #446

Closed metagn closed 2 years ago

metagn commented 2 years ago

There are 2 syntaxes for exporting newly defined symbols in current Nim.

  1. Postfix export marker:
let foo* = 3
proc foo* = discard
type Foo* = object
  foo*: int
  1. Export statement:
export Foo, foo
export currentmodulename # ???

These have some weaknesses:

  1. Postfix export marker: Hard to replicate in macros (class Foo*: is impossible). Complicates the parsing of things like tuple unpacking syntax. Complicates macros as well when used, as they have to account for its unique syntax.
  2. Export statement: Does not work for every kind of symbol (fields for example), verbose, not very pretty. Also is a statement, so needs own line and cannot be replicated easily in macros.

The proposal is an additional syntax which also has weaknesses and is redundant in places; but is not super foreign, does not break existing code and compensates for the other export syntaxes.

Proposal

The export keyword can also be used as a pragma to export symbols it is attached to.

let foo {.export.} = 3
proc foo {.export.} = discard
type Foo {.export.} = object
  foo {.export.}: int

Advantages

Criticisms

mratsim commented 2 years ago

Having one way to two ways to do things in macros would seriously complicate their maintenance and lead to spaguetti if/elif/else

I don't see what's complex with the postfix marker. There is plenty of examples out there that exist today. If anything "How-to" are needed. If we now have both ways, writing an "How-to" will be even more complex.

Lastly you can write a template or macro as pragma to do what you want.

metagn commented 2 years ago

There were already some defenses to those statements in the post (except yes postfixes aren't really complex), and I would expand further, but yeah this is an unproductive proposal

Araq commented 2 years ago

The export method is for forwarding of symbols that stem from an imported module. It was never anticipated as an alternative to the export marker, that was a side-effect of the implementation that then got exploited.