di
di copied to clipboard
Convert constants to values at compile time
In this moment if you write constant to neon like this
myExtension:
languageId: ::constant(MyLanguageEnum::CZECH)
class
class MyLanguageEnum
{
public const CZECH = 1;
}
In compile time I get Statement object
arguments =>
0 => "MyLanguageEnum::CZECH"
entity private => "::constant"
In my use case I need value of constant, here is not problem do it.
I convert constant to value in compile time like this:
class NeonConstantToValue
{
use StaticClass;
public static function run(Statement $statement)
{
if ($statement->getEntity() !== '::constant') {
return $statement;
}
return constant(reset($statement->arguments));
}
}
If I thinking about it, the constants can be convertet to value by nette in compile time everytime.
If i made benchmark of this solution that is faster, because it has raw value instead of call function constant with parameter string like this 'languageId' => constant('MyLanguageEnum::CZECH'). The goal is 'languageId' => 1.
What do you think?
It does not seem to me that it is measurably faster.
It is secondary litle reason, i try 10k cycle where is cca 4x faster.
0.0012 -> function constant
0.00026 -> raw value
First idea was: Is posible in compile time use raw value insteand of Statement for constant?
It can be done, but it's a BC break. Because someone can rely on the current way.
It's probably slower because function call is used. So what if we instead of constant('MyLanguageEnum::CZECH') use directly MyLanguageEnum::CZECH? Uses actual value of constant and reference to source is not lost.
I need value of constant in compile time. This MyLanguageEnum::CZECH is not solution for my use case, still i must keep external method what convert constant to value.
What about keep Statement and add new property with value? Is not ideal, because is only for ::constant.
What about new class StatementConstant extend Statement and ovewrite behavior? This couldn't be BC break?
One point
If you use direct constant like describe @mabar, that is safer if value changed. Because if set raw value and change constant. In this moment nette does not rebuild container.