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

Nim reflect on type's field types at compile-time
Save variables to file at runtime
In Julia, is it possible to pass values for evaluation in an Expr object without using global variables?
Metaprogramming in Julia — Splice integer into variable name
What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string
Pattern matching benchmarking : Compiletime lookup vs Runtime lookup in D
Meta construction capabilities?
How to manipulate the value of variables based on an array of symbols in Julia?

Categories

HOME
android
oracle12c
formal-languages
jsf-2.2
shiro
grafana
cakephp-3.4
zip
azure-container-service
title
special-characters
augeas
ibeacon-android
emoji
symmetricds
gource
http-post
template-engine
imessage
google-container-registry
drive
ksh
inno-download-plugin
flask-sqlalchemy
resourcebundle
xcode-ui-testing
nstimer
inversion-of-control
spotipy
paragraph
cart
android-gridview
biztalk-rule-engine
instantiation
vue-chartjs
nested-lists
engine.io
jpad
tex
nohup
projects-and-solutions
bc
skylink
microsoft-metro
file-sharing
servermanager
serde
corruption
bbpress
juice-ui
cronexpression
mapxtreme
pacemaker
lrs
gogs
php-socket
ikiwiki
dock
sql-server-data-tools
avahi
actionfilterattribute
adehabitathr
mass-assignment
beatsmusic
addressing-mode
entry-point
named-parameters
paste
vsx
websphere-esb
synapse
drupal-nodes
light
http-patch
jammer
rolify
kaleidoscope
joomla-template
pclzip
http-status-code-410
resource-management
pdfviewer
android-loadermanager
sentestingkit
boost-msm
dajaxice
httponly
isa-swizzling
screen-size
commerceserver2007
blender-2.49
burndowncharts
lxr
windows-identity
scrubyt
contentpresenter

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