I tried some code in the same vein as the example in the manual in the "implicit generics" section, and I am not sure if below is buggy behavior.
# create a type class that will match all tuple and object types
type RecordType = tuple or object
proc printFieldsImplicit(r: RecordType) = discard
proc printExplicit[T: tuple or object](r : T) = discard
printExplicit((4,5)) # no complaint
printFieldsImplicit((4,5)) # won't compile
I get this error
Error: type mismatch: got <(int, int)>
but expected one of:
proc printFieldsImplicit(r: RecordType)
first type mismatch at position: 1
required type for r: RecordType
but expression '(4, 5)' is of type: (int, int)
expression: printFieldsImplicit((4, 5))
which seems odd seeing as RecordType can be a tuple and (int, int) should be tuple type? This is actually an issue with the type RecordType = tuple or object the following works:
# create a type class that will match all tuple and object types
type RecordType = (tuple or object)
proc printFieldsImplicit(r: RecordType) = discard
proc printExplicit[T: tuple or object](r : T) = discard
printExplicit((4,5)) # no complaint
printFieldsImplicit((4,5)) # won't compile
@ElegantBeef watch out, you did not update your comment in your code and say it won't compile when it does.
Parenthesis are not mandatory when specifying tuple arguments. I tried:
type RecordType = (tuple[a: int, b: int] or object)
Is it a semcheck error ? Is there a guide (in addition to the std/manual) that could point to common mistakes in code ? It would help to have a comprehensive guide of common error messages in Nim and their code equivalent. I don't want to list all possible beginner's code, just list edge cases like this one that are hard to remember.
If you want, I can make a nimib for that, I just do not have a blog/webserver to publish and share it.
If you want, I can make a nimib for that, I just do not have a blog/webserver to publish and share it.
the simplest way would be to just create a github repo, commit the html and publish with github pages (like in nblog), better but more complex would be to use nimibook (like in scinim/getting-started and the nice thing is to have CI build docs and deploy preview, but it takes more effort). I do plan to improve the state of blogging with nimib (my next priority for nimib activities).
Can confirm that if I do
type RecordType = object or tuple
it compiles. Hey dont look at me! Ideally the original post would've been the following. :P
static:
assert compiles(printExplicit((4,5)))
assert not compiles(printFieldsImplicit((4,5))