Use case:
I'm trying to make a really basic sdl2 library from scratch, that maps directly to the C version.
The C code declares this function:
Sdl_Quit
But there's also this constant:
SDL_QUIT
Due to Nim's style-insensitivity, I can't have both of these identifiers as is. eg they both start with a caps S, and otherwise look the same in terms of identifier name to the Nim compiler
Possible to get some kind of pragma to use strict styling rather than flexible styling?
eg something like this;
{.push strictStyle.}
proc Sdl_Quit() = ...
const SDL_QUIT = ...
{.pop strictStyle.}
Then those identifiers only ever get used in the code if you use the exact same naming, rather than eg, nim thinking the two are the same identifier.
Comments?
PS: My current temporary workaround is to name the SDL_QUIT constant, SDL_QUIT_CONST. But that means that I can't get a direct 1-1 mapping between C code and nim code (for instance).
Or is there perhaps some existing method, eg stropping, which already handles this kind of problem?
Nope, what you are looking for does not exist. All you can do is rename the Nim version of that Identifier. Be aware there are already two sdl2 wrappers around. I don't think that yet another will help anybody except yourself in learning how to write wrappers.
https://github.com/Vladar4/sdl2_nim https://github.com/nim-lang/sdl2
Wrappers so far are mainly for my own private testing use, rather than for something to publish :-)
Basically, going through the lazyfoo tuts, but transliterating as closely as possible from the original C over to Nim
eg, code snippets like these, from the C version of chapter 3:
http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
And
//Quit SDL subsystems
SDL_Quit();
I'd be trying to write directly like this in Nim:
# User requests quit
if e.`type` == SDL_QUIT:
quit = true
And
# Quit SDL subsystems
SDL_Quit()
Mainly it means that for my direct translation from C to Nim, that one snippet needs to change to this for me:
# User requests quit
if e.`type` == SDL_QUIT_CONSTANT:
quit = true
Was mainly wondering if there's a way to not need to do that kind of renaming while doing direct transliterations from other languages to Nim :-)
IDK, maybe your approach can be helpful on a low-level layer but why would I want writing C in Nim? I'd prefer event type as enum and sdl_quit declared as just quit. It can be used as sdl2.quit, assuming your wrapper code's module name is sdl2, then we can have auto-completion, like
sdl2.nim
type
event_type* = enum
quit_event ,
keyboard_event ,
joy_button_event ,
mouse_motion_event
event* = object
etype* : event_type
proc quit* =
discard
test1.nim
from sdl2 import nil
var
e : sdl2.event
if e.etype == sdl2.quit_event:
sdl2.quit()
If the enum that defines SDL_QUIT is marked {.pure.}, then you have to refer to SDL_QUIT as fully qualified
type
sdlEv {.pure.} = enum
...
SDL_QUIT,
...
if e.`type` == sdlEv.SDL_QUIT:
SDL_Quit()
It makes your code much more verbose, and may not be style you want your code to be, but may be an option.
Also note, in writing this in Nim, should try for NEP1 coding style to be consistent with other Nim programs (but then not consistent with SDL C .... ;-) and yes, my example wasn't NEP1 either)