gpt4 book ai didi

Ruby:定义函数原型(prototype)?

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:07 24 4
gpt4 key购买 nike

可以定义一个函数的原型(prototype),或者以某种方式向 Ruby 表明一个函数存在,即使它可能还没有被定义?

我有很多这样的类(class):

class Program 
FIRST = Block.FIRST
FOLLOW = Set.new['$']
end

class Block
FIRST = Declaration.FIRST
FOLLOW = Set.new['.']
end

class Declaration
FIRST = ConstDecl.FIRST + VarDecl.FIRST + ProcDecl.FIRST
end

class ConstDecl
FIRST = Set.new['const'] + EMPTY_SET
end

您可以看到在它们下面定义的类的引用字段,有没有办法向 Ruby 指示这些类存在,并要求 Ruby 查找它们?

最佳答案

我能想到的最简单的方法是这样的:

class Program
def self.first; Block.first end
def self.follow; Set.new['$'] end
end

class Block
def self.first; Declaration.first end
def self.follow; Set.new['.'] end
end

class Declaration
def self.first; ConstDecl.first + VarDecl.first + ProcDecl.first end
end

class ConstDecl
def self.first; Set.new['const'] + EMPTY_SET end
end

不过,这对我来说似乎不是一个好的设计,我可能会制作这些对象而不是类,并使用适当的类型层次结构。

关于Ruby:定义函数原型(prototype)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com