nim-lang / compilerdev

This repository contains a collection of documents about how to change/refactor the Nim compiler in order to make it faster, easier to maintain and have fewer bugs by a superior architecture and design. However, no every idea here will work out.
10 stars 3 forks source link

make `proc nimQuery(setting: Setting): RootObj {.magic.}` => return a type that depends on the enum #9

Open timotheecour opened 4 years ago

timotheecour commented 4 years ago

@araq ok with following?

proposal

example

doAssert nimQuery(nimVerCT) == (1,3,7)
  # I need this in many places, after this we don't need to define new symbols in condsyms anymore since we can test for nimVerCT
  # i had originally introduced it in https://github.com/nim-lang/Nim/pull/14447#issuecomment-633725338 but moved it out of there
doAssert nimQuery(assertions) # can replace `compileOption("assertions")`
doAssert nimQuery(backend) == Backend.js
doAssert nimQuery(arguments) is seq[string]
doAssert Experimental.notnil in nimQuery(experimental)

note

compilesettings stays a "low-level" module in the sense it's usable by low level modules (eg doesn't depend on iterators, dollars etc)

implementation

no particular difficulty

Araq commented 4 years ago

Oh my, please ... no. I think it's a bad design, reminds me of sysctl. It's faking simplicity.

timotheecour commented 4 years ago

i was half expecting that, and I kind of agree. Then this?

proc nimVerCT*(): tuple(int,int,int)
proc getExperimental*(): seq[Experimental] # type Experimental = enum ...
proc getBackend(): Backend
when nimVerCT() < (1,3,5) : ...
when Experimental.notnil in getExperimental(): ...
Araq commented 4 years ago

What's wrong with the existing solutions?

timotheecour commented 4 years ago

This is more user friendly, self documenting and impossible to misuse or you'll get CT error:

# either the "sysctl"-like way i gave in top post, or as individual procs, I'm agnostic:
doAssert nimVerCT() == (1,3,5)
doAssert Experimental.forLoopMacros in getExperimental()
doAssert assertions() # or maybe `nimQuery(assertions)` still in this case
Araq commented 4 years ago

Well as long as it doesn't bloat system.nim further, I'm fine with more type-safe additions.