metaprogramming


Nim reflect on type's field types at compile-time


Suppose I have a type defined as follows:
type TMyStruct = object
foo: int32
bar: int16
My goal is to construct a macro (or otherwise) which, given a "simple" object type like the one above, is able to compute the sum of the sizes of each field in the type, based on sizeof. In this case int32 has size 4 and int16 has size 2 so the idea is that
myMacro(TMyStruct) # or, in the worst case, myMacro(x) where x is a TMyStruct
should evaluate to 6 as a constant expression. I want to extend it to nested objects later on but that should be easy via recursion once the basic version is working.
I have tried many things and failed horribly; the farthest I managed to get was to retrieve the field names "foo" and "bar" in the AST as nnkSymNodes, but I was unable to retrieve any information about their types. Meaningful documentation (at my level of Nim expertise) is sparse to non-existent.
Is what I am asking possible, and what Nim functionality do I need to use to achieve it?
Thanks
import macros
type TMyStruct = object
foo: int32
bar: int16
macro sumSizes(t: typedesc): expr =
result = nil
let tDesc = getType(getType(t)[1])
for field in tDesc[2].children:
let sizeOfThis = newCall("sizeof", field)
if isNil(result):
result = sizeOfThis
else:
result = infix(result, "+", sizeOfThis)
echo sumSizes(TMyStruct)
It is a bit strange that you have to chain the getType calls in the beginning. This is because getType(t) returns the following:
BracketExpr
Sym "typeDesc"
Sym "TMyStruct"
If you want to do this with instances of a type, just change these lines:
macro sumSizes(e: typed): expr =
result = nil
let tDesc = getType(e)
Note that typed is important here to have a typed symbol inside the macro.

Related Links

Elixir - Define functions in module by metaprogramming
Disambiguate the various quoting mechanisms in Julia metaprogramming
How to initialize list using macros and then generate same set of functions in 2 different modules?
Type-safe metaprogramming? Using strings to query ceylon.language for meta information?
Interpolating an Expression into an Expression
Loop unrolling? in Julia with metaprogramming
Macro for a function template: escaping issues
Macro that defines macro returning struct constant
Array of Expressions to an Array Definition
Code generation from restricted set of input
System of equations using metaprogramming
How to define a partial copy constructor metaprogramically in Julia?
How to interpolate into a Julia “for” expression?
Sending a keyword list to a macro and using bind_quoted
Runtime meta programming in ceylon
Is there a fast way of going from a symbol to a function call in Julia? [duplicate]

Categories

HOME
batch-file
scikit-learn
facebook-oauth
formal-languages
mysql-workbench
hessian
cublas
3d-reconstruction
pyqt4
nested-if
python-multithreading
code-coverage
google-admin-sdk
jtextpane
angularfire2
retrofit2
crystal-lang
fosrestbundle
windows-ce
onedrive-api
heat
searchview
symfony-2.8
vuejs
google-picker
ag
poedit
libigl
gulp-watch
r-mice
event-store
gmock
interbase
custom-post-type
fable-f#
oculus
handler
paperjs
magento-1.8
xbox
catch-unit-test
getlasterror
odroid
p4merge
outputcache
x++
cnc
reportservice2010
subnet
truezip
cd-burning
django-1.10
slackware
redistributable
redbeard-ios
mta
avspeechsynthesizer
simevents
mediametadataretriever
fuzzywuzzy
contentcontrol
maybe
tfs-workitem
rate-limiting
phonertc
dock
cgi-bin
doctype
zscript
teamcity-9.1
bcache
dbaccess
execve
uncaught-typeerror
template-deduction
dct
eclipse-kepler
breakout
model-driven-development
qtconcurrent
subproject
extconf.rb
httponly
build-environment
todos
entitykey
movieplayer
commercial-application

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App