compiler-errors - Eiffel : once function has generic or anchored result. Trying to create constants

标签 compiler-errors constants eiffel class-constants

我正在尝试创建一些终端日志记录颜色,以便能够更清楚地看到我的错误。这样做对我来说似乎很明显,我必须创建如下的常量。因为我不想每次调用常量时都创建一个实例,所以对我来说像这样做是有意义的,但是编译器似乎与我的概念不同……
A once function has generic or anchored result 什么是anchored结果

由于编译器始终是硬道理,而我则是最重要的人,为什么我错了,他是对的吗?

class
    TERMINAL_COLOR

create
    make

feature -- Initialization

    make (a_fg: like foreground; a_bg: like background)
        do
            foregound := a_fg
            background := a_bg
        end

feature -- Status report

    foreground: INTEGER

    background: INTEGER

feature -- Colors

    Black: like Current
        once -- compiler doesn't agree with me
            create Result.make (30, 40)
        ensure
            instance_free: class
        end

end

最佳答案

anchor 类型是当您使用“喜欢功能”(注意,您也可以使用“喜欢{FOO} .bar”)。

另外,不要忘记一次是“每堂课一次”(不是按类型)。
因此,一次函数的结果类型不能使用任何形式的泛型。
例如

class FOO [G]
feature
    bar: STRING
        once
           Result := generating_type
        end
end
(create {FOO [INTEGER]}).bar将返回与(create {FOO [STRING]}).bar相同的对象。

因此,现在,如果bar将在类FOO中返回G,则会引起麻烦,因为无法返回符合任何形式(INTEGER,STRING等)的值。

这就是为什么一次结果类型禁止泛型的原因。

相同的逻辑适用于 anchor 类型(例如like feature_name),因为feature_name可以与其他类型的后代一起重新定义。

关于compiler-errors - Eiffel : once function has generic or anchored result. Trying to create constants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52857596/

相关文章:

java - 如何在Eclipse中自定义并触发Java编译错误

windows - 如何使 Eiffel Vision2 窗口始终位于顶部?

c++ - Eiffel 工作室中的 "Unable to store the editor layout information."

facebook - React Native 错误 - yarn' 未被识别为内部或外部命令

linux - 为什么我需要在参数中添加 const?

c# - 第一次使用msbuild

const unsigned char[][] 的 C 数组

c - 在 C 中将 & (addressof) 与 const 变量一起使用

c++ - 声明数组时 VS2008 错误预期常量表达式,但 GCC 中此代码没有错误

immutability - Eiffel 中的不可变类