constant

constant(string $name): mixed

按名字符串返回常量的值(常量的运行时反射查询)。

C C23

C23
#include <stdio.h>
#include <string.h>
#define APP_VERSION "1.0"

int main(void) {
    const char *name = "APP_VERSION";
    // 宏在预处理后消失,不进符号表;按名取值只能手写映射
    if (strcmp(name, "APP_VERSION") == 0) {
        printf("%s\n", APP_VERSION);
    } else {
        printf("?\n");
    }
    return 0;
}
输出
1.0

C++ C++26

C++26
#include <cstring>
#include <iostream>

// C++ 无常量反射;同样靠手写名字映射兜底
const char *constant(const char *name) {
    if (std::strcmp(name, "APP_VERSION") == 0) return "1.0";
    return "?";
}

int main() {
    std::cout << constant("APP_VERSION") << "\n";
    return 0;
}
输出
1.0

C# v11.0.100

v11.0.100
using System;
using System.Reflection;

var value = typeof(App).GetField("AppVersion")!.GetValue(null); // 反射可读取 const 字面量字段
Console.WriteLine(value);

static class App
{
    public const string AppVersion = "1.0";
}
输出
1.0

Go v1.27.4

v1.27.4
package main

import "fmt"

const AppVersion = "1.0"

func main() {
	// Go 的 const 不进运行时符号表,按名取值需自建映射
	table := map[string]string{"AppVersion": AppVersion}
	fmt.Println(table["AppVersion"])
}
输出
1.0

Java v27.0.1

v27.0.1
import java.lang.reflect.Field;

public class Main {
    static final String APP_VERSION = "1.0";

    public static void main(String[] args) throws Exception {
        Field f = Main.class.getDeclaredField("APP_VERSION"); // 反射按名读取静态字段
        System.out.println(f.get(null));
    }
}
输出
1.0

JavaScript v26.13.1

v26.13.1
// 模块作用域的名字无法反射;常量表对象是 JS 的惯用替代
const CONSTANTS = { APP_VERSION: "1.0" };
const name = "APP_VERSION";
console.log(CONSTANTS[name] ?? "?");
输出
1.0

PHP v8.6.0

v8.6.0
define("APP_VERSION", "1.0");
$name = "APP_VERSION";
echo constant($name), "\n", defined($name) ? "yes" : "no";
输出
1.0
yes

Python v3.15.2

v3.15.2
APP_VERSION = "1.0"
name = "APP_VERSION"
print(globals().get(name, "?"))  # globals() 按名字符串直接反查
输出
1.0

Rust v1.99.1

v1.99.1
// Rust 无运行时反射;match 手写名字分派
fn constant(name: &str) -> Option<&'static str> {
    match name {
        "APP_VERSION" => Some(APP_VERSION),
        _ => None,
    }
}

const APP_VERSION: &str = "1.0";

fn main() {
    println!("{}", constant("APP_VERSION").unwrap());
}
输出
1.0

TypeScript v26.13.1

v26.13.1
// 模块作用域的名字无法反射;常量表对象 + 索引访问
const CONSTANTS: Record<string, string> = { APP_VERSION: "1.0" };
const name = "APP_VERSION";
console.log(CONSTANTS[name] ?? "?");
输出
1.0

语言差异说明

C:宏在预处理阶段展开完毕,根本不进符号表——按字符串名取值没有语言支持,只能手写名字→值映射(示例)。

C++:constexpr 值编译期确定但同样无名字反射;运行时按名查询需自建映射函数(示例);RTTI 只覆盖类型不覆盖具名常量。

C#:有真反射——const 字段编译为字面量静态字段,GetField(...).GetValue(null) 可读取(示例);跨程序集注意常量已被内联。

Go:const 在编译期求值后不进运行时符号表,无反射可查;惯用替代是自己维护一张名字→值的映射表(示例)。

Java:反射能力完整——getDeclaredField + Field.get(null) 可按名读取静态常量字段(示例);static final 的原始类型/字符串在编译处会被内联,但反射读到的仍是字段原值。

JavaScript:const/let 的词法绑定对反射不可见——无法按字符串名访问模块作用域变量;惯用替代是把常量收进一个可索引的对象表(示例)。

PHP:constant 是常量的运行时反射入口——名字可以是任意字符串表达式,常与 defined 配合做存在性预检(示例);未定义的常量查询抛 Error。

Python:模块级名字就是字典条目,globals()[name] 直接按字符串反查(示例)——Python 的「反射」能力天然最强。

Rust:无运行时反射;按名分派只能 match 手写(示例),常量在编译期已内联进使用处。

TypeScript:运行时同 JavaScript(词法绑定不可反射),常量表 + Record 索引是惯用替代(示例);类型层面仅编译期可见。