所以我正在构建一个数据类型,我想, optional auto-casting . The last question I asked is related to this also.
我目前拥有的代码可以在下面找到:
class Test(T)
@@auto_cast = false
def initialize(var : T)
@var = var
end
def self.auto_cast
@@auto_cast
end
def self.auto_cast=(val)
@@auto_cast = val
end
def self.auto_cast(forced_value=true,&block)
#Force value, but store initial value:
ac = @@auto_cast
@@auto_cast = forced_value
block.call
@@auto_cast = ac
end
def +(val)
var = @var
if @@auto_cast
if var.is_a? String
casted_arg = val.to_s
return var + casted_arg
else
casted_arg = typeof(var).new(val)
return var + casted_arg
end
else
if typeof(var) != typeof(val)
{{raise "Error: Type of <<var>> is not equal to type of <<val>> while auto_cast is false."}}
else
return var + val
end
end
end
end
当我尝试测试数据类型时:
Test.auto_cast do
puts Test.auto_cast
puts Test.new(1) + "1"
puts Test.new("1") + 1
end
它会在 return var + val
处抛出错误:
if typeof(var) != typeof(val)
{{raise "Error: Type of <<var>> is not equal to type of <<val>> while auto_cast is false."}}
else
ERROR! --> return var + val
end
起初我很困惑为什么,但现在它是有道理的 .
-
我相信每当我打算使用auto_cast时,Crystal编译器都无法确定
@@auto_cast
是否为真(并且公平地说,当禁用自动转换时,我希望语法错误) . -
发生编译错误,因为
@@auto_cast
的值在编译时未知 . -
由于尸体的矛盾性质:
.
if var.is_a? String
casted_arg = val.to_s
return var + casted_arg
else
casted_arg = typeof(var).new(val)
return var + casted_arg
end
和
if typeof(var) != typeof(val)
{{raise "Error: Type of <<var>> is not equal to type of <<val>> while auto_cast is false."}}
else
return var + val
end
每个定义只应在用户明确声明时使用 . 因此,这更适合宏观 .
鉴于这些原因,我开始尝试将功能构建到宏中:
def +(val)
var = @var
{%if @@auto_cast%}
if var.is_a? String
casted_arg = val.to_s
return var + casted_arg
else
casted_arg = typeof(var).new(val)
return var + casted_arg
end
{%else%}
if typeof(var) != typeof(val)
{{raise "Error: Type of <<var>> is not equal to type of <<val>> while auto_cast is false."}}
else
return var + val
end
{%end%}
end
我认为这样可行,因为这样只有在设置 @@auto_cast
时才会生成代码 . 然而,我忘记的是前提#2 . I.E. @@auto_cast
的值在编译时是未知的 . 最终,为了完成这项工作,我需要一个变量,它可以是:
-
在编译时设置 .
-
在编译时在宏中全局使用 .
最终我认为我可以按照以下方式做点什么:
SET_AUTOCAST_VARIABLE true
puts Test.auto_cast
puts Test.new(1) + "1"
puts Test.new("1") + 1
SET_AUTOCAST_VARIABLE false
然后在 +()
定义中:
{%if autocast_variable %}
...
{%else%}
...
{%end%}
问题是,我不认为这样的宏全局变量存在......我正在考虑如何解决这个问题,到目前为止,我能想到的唯一解决方案是在编译时使用一些外部文件:
{{File.write("/tmp/cct","1")}}
puts Test.auto_cast
puts Test.new(1) + "1"
puts Test.new("1") + 1
{{File.write("/tmp/cct","")}}
并在方法定义中:
{%if File.read("/tmp/cct")=="1" %}
...
{%else%}
...
{%end%}
虽然这感觉真的很糟糕......我想知道是否有其他选择,(或者,甚至,如果这根本不起作用)?
1 回答
这不行 . 方法仅实例化一次,不可能具有相同参数类型的相同方法的两个实现 . 在以下示例中,两个
+
方法将不可避免地具有相同的实现 .根据词法范围,您不能对同一方法有不同的实现 . 该方法完全实例化一次,其中的宏也是如此 .
我不了解您的整体用例,但也许还有其他方法可以实现您的需求 .
为了完整性:您可以将常量用作宏全局变量 . 常量不能重新定义,而是通过宏表达式改变 . 这可以用于在宏之间存储状态 . 例如:
不过,那真是太烂了;)