Skip to content

Instantly share code, notes, and snippets.

@monoman
Last active September 25, 2015 09:38
Show Gist options
  • Save monoman/901638 to your computer and use it in GitHub Desktop.
Save monoman/901638 to your computer and use it in GitHub Desktop.
Boo's creator answering a question on Boolang list. Another wonder of meta-programming...
Daniel Brauer's Question:
Of course I just figured it out: I was trying to declare a bunch of floats like this:
[SerializeField]
m_forwardPower = 2f,
m_backwardPower = 1f,
m_strafePower = 0f,
m_maxVelocity = 5f,
m_turnSpeed = 3f,
m_maxRollAngle = 10f,
m_rollRate = 6f
So now my question is whether there's an elegant way to put the SerializeField attribute on all
of those floats without having to type it out for each one.
---------------------------------------------------------------------------------------------------
Rodrigo B. de Oliveira's answer:
Something like this:
import Boo.Lang.Compiler
import Boo.Lang.Compiler.Ast
import Boo.Lang.PatternMatching
import Boo.Lang.Compiler.MetaProgramming
macro serializeFields:
for statement in serializeFields.Body.Statements:
match statement:
case ExpressionStatement(Expression: [| $fieldName =
$initializer |]):
yield [|
[SerializeField]
private $fieldName = $initializer
|]
code = [|
import System
class Foo:
serializeFields:
m_forwardPower = 2f
m_backwardPower = 1f
m_strafePower = 0f
m_maxVelocity = 5f
m_turnSpeed = 3f
m_maxRollAngle = 10f
m_rollRate = 6f
|]
result = compile_(code, System.Reflection.Assembly.GetExecutingAssembly())
print result.Errors.ToString(true)
print result.CompileUnit.ToCodeString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment