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

what if I don't want the default behaviour of enum ? #77

Open
hamidb80 opened this issue Aug 26, 2023 · 2 comments
Open

what if I don't want the default behaviour of enum ? #77

hamidb80 opened this issue Aug 26, 2023 · 2 comments

Comments

@hamidb80
Copy link

hamidb80 commented Aug 26, 2023

Hey,

I just want to store enums as ints,

wrapper.nim:

import jsony
import inner

type
    Enum = enum
        e1
        e2

echo fromJson("0", Enum)

inner.nim

import std/[parseutils]

proc parseHook*[T: enum](s: string, i: var int, v: var T) =
  var temp: int
  inc i, parseInt(s, temp, i)
  v = T temp

proc dumpHook*(s: var string, v: enum) =
  s.add $v.int

Expected behaviour

compiles successfully and considers enums as ints

Current output

/wrapper.nim(10, 14) template/generic instantiation of `fromJson` from here
/.nimble/jsony/jsony.nim(590, 4) 

Error: ambiguous call; both 

jsony.parseHook(s: string, i: var int, v: var T: enum) [proc declared in /.nimble/jsony/jsony.nim(406, 6)] 
and 
inner.parseHook(s: string, i: var int, v: var T: enum) [proc declared in inner.nim(3, 6)] 

match for: (string, int, Enum)
@hamidb80 hamidb80 changed the title what if I don't want the default behaviour of enum ? [RESOLVED] what if I don't want the default behaviour of enum ? Aug 26, 2023
@hamidb80 hamidb80 changed the title [RESOLVED] what if I don't want the default behaviour of enum ? what if I don't want the default behaviour of enum ? Aug 27, 2023
@hamidb80 hamidb80 reopened this Aug 27, 2023
@hamidb80
Copy link
Author

hamidb80 commented Aug 27, 2023

mixing wrapper.nim and inner.nim works fine, but I cannot separate them

as a quick workaround I made a file called jsony_fix.nim:

import std/parseutils
proc parseHook*[T: enum](s: string, i: var int, v: var T) =
  var temp: int
  inc i, parseInt(s, temp, i)
  v = T temp

proc dumpHook*(s: var string, v: enum) =
  s.add $v.int

and include it wherever I import Jsony:

import jsony
include jsony_fix

@treeform
Copy link
Owner

treeform commented Sep 8, 2023

Hey,

This is because you are trying to override a library function precisely. I'm not sure what the best solution is. If you had proc parseHook*[MyEnum], it would work. But you seem to want to override the general one. I'm not certain Nim supports this. I'm glad you found a workaround.

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

No branches or pull requests

2 participants