# OptionValue not inserting correct option

I am using OptionsPattern and OptionValue to define some switches for my custom function. Here is a minimal example:

t[vars_, expr_,
OptionsPattern[{ ExperimentalOptimizeExpression,
Compile}]] :=
(
Compile[vars, expr,
CompilationOptions -> OptionValue[CompilationOptions],
CompilationTarget -> OptionValue[CompilationTarget],
Parallelization -> OptionValue[Parallelization],
RuntimeAttributes -> OptionValue[RuntimeAttributes],
RuntimeOptions -> OptionValue[RuntimeOptions]
]
)
t[{x, y}, Sin[x*y]]


This produces the errors:

"Options expected (instead of \
OptionValue[{ExperimentalOptimizeExpression,Compile},{},\
CompilationOptions]) beyond position 2 in \
Method->{OptionValue[{ExperimentalOptimizeExpression,Compile},{},\
CompilationOptions]}. An option must be a rule or a list of rules."

"Options expected (instead of \
OptionValue[{ExperimentalOptimizeExpression,Compile},{},\
RuntimeOptions]) beyond position 2 in \
Method->{OptionValue[{ExperimentalOptimizeExpression,Compile},{},\
RuntimeOptions]}. An option must be a rule or a list of rules"

"OptionValue[{ExperimentalOptimizeExpression,Compile},{},\
RuntimeAttributes] is not a known runtime attribute and will be \
ignored"


and fails to execute with default values passed to the options. OptionValue appears to fail to grab the correct value. (Moreover, Method appears to be being assigned a value even though Compile has no such option, but this could just be internal definitions to Compile)

Assigning explicitly something like

t[{x, y}, Sin[x*y],
CompilationOptions -> {"ExpressionOptimization" -> True}]


produces similar errors. Why isn't the correct OptionValues being parsed?

## 1 Answer

Compile holds its arguments, and doesn't recognize the unevaluated Rule[_, _OptionValue] as an option. Use With to inject the options into Compile:

t[vars_, expr_, OptionsPattern[{ExperimentalOptimizeExpression, Compile}]] := With[
{
opts = {
CompilationOptions -> OptionValue[CompilationOptions],
CompilationTarget -> OptionValue[CompilationTarget],
Parallelization -> OptionValue[Parallelization],
RuntimeAttributes -> OptionValue[RuntimeAttributes],
RuntimeOptions -> OptionValue[RuntimeOptions]
}
},
Compile[vars, expr, opts]
]
`