是否有一种编程语言可以重新定义数字?

问题描述 投票:1回答:1

我对这个问题的第一个突破是在C中

#define 2 5
assert(2+2 == 10);

不幸的是

error: macro name must be an identifier

我也尝试过Scheme

(define 2 5)

但是

can't define a non-symbol: (define 2 5)

我想知道是否有任何可能的编程语言。

programming-languages flexibility
1个回答
2
投票

我真诚地希望不会。

但是,我确实知道Java的一种方法:您可以使用reflection来调整已缓存的带框Integer的值:JVM必须缓存-128到+127范围内的所有值,并且确实存在一种机制可以调整该缓存中的数值!

有关更多详细信息,请参见https://codegolf.stackexchange.com/questions/28786/write-a-program-that-makes-2-2-5/28818#28818。这是完整的代码:

import java.lang.reflect.Field;
public class Main {
    public static void main(String[] args) throws Exception {
        Class cache = Integer.class.getDeclaredClasses()[0];
        Field c = cache.getDeclaredField("cache");
        c.setAccessible(true);
        Integer[] array = (Integer[]) c.get(cache);
        array[132] = array[133];

        System.out.printf("%d", 2 + 2);
    }
}

输出为5,基本上是通过重新定义数字4实现的。

© www.soinside.com 2019 - 2024. All rights reserved.