简体   繁体   English

以任何编译语言实现以下语法(适用于Factory设计模式)吗? 最好是Kotlin,C ++

[英]Achieve the below syntax(for Factory design patters) in any compiled language? preferably Kotlin, C++

I want to know if the below syntax is available for a compiled langue? 我想知道以下语法是否可用于编译语言? Please do not provide java as it requires the JVM. 请不要提供Java,因为它需要JVM。 The design is equivalent to a factory design pattern as we initialize a class once some test passes in normal situations we use lots of if-else statements ie When we have a range of classes we ie 10 classes the code becomes clunky I am trying to create a language transpiled or compiled not decided on that yet and would looking into features capabilities of the already compiled languages currently working with kotlin-native and llvm (not yet sorted llvm integration) it's on startup and that is what I want to consider using. 当我们在正常情况下通过一些测试后初始化一个类时,该设计等效于一种工厂设计模式,我们使用大量的if-else语句;即,当我们拥有一系列的类时,即10个类,代码变得笨拙,我试图创建一种尚未翻译或编译的语言尚未决定,并且会考虑目前正在与kotlin-native和llvm(尚未排序的llvm集成)一起使用的已编译语言的功能,这是启动时需要考虑的。

 class A{ } class B extends A{ } class C extends A{ } class D extends A{ } class E extends A{ } class F extends A{ } class G extends A{ } class H extends A{ } class I extends A{ } class J extends A{ } class K extends A{ } function getClass(test){ if(test == 1){ return new B() }else if (test == 2){ return new C() }else if (test == 3){ return new D() }else if (test == 4){ return new E() }else if (test == 5){ return new F() }else if (test == 6){ return new G() }else if (test == 7){ return new H() }else if (test == 8){ return new I() }else if (test == 9){ return new J() }else if (test == 10){ return new K() } } let klass = getClass(10) console.log(`Found class ${klass.constructor.name}`) 

 class A { static test(test) { return test === this.index; } } class B extends A{} B.index = 1 class C extends A{} C.index = 2 class D extends A{} D.index = 3 class E extends A{} E.index = 4 class F extends A{} F.index = 5 class G extends A{} G.index = 6 class H extends A{} H.index = 7 class I extends A{} I.index = 8 class J extends A{} J.index = 9 class K extends A{} K.index = 10 let klasses = [B, C,D,E,F,G,H,I,G,K]; let klass = new A(); let test = 7; for (let i = 0; i < klasses.length; i++) { try { if(klasses[i].test(test)){ klass = new klasses[i]() } } catch (e) { console.log(e) } } console.log(`Found class ${klass.constructor.name}`); 

Can you clarify your question? 你能澄清你的问题吗? Your previous question How can I achieve the below syntax in any compiled language? 您之前的问题如何以任何编译语言实现以下语法? preferably Kotlin, C++ actually seemed clearer to me. 最好是Kotlin,C ++在我看来实际上更清晰。 Could you maybe give us information on what high-level goal you are trying to achieve? 您能否为我们提供有关您要实现的高层目标的信息?

Are you trying to design a new language? 您是否正在尝试设计一种新语言? Are you trying to pick a language to implement your new language's parser in? 您是否正在尝试选择一种语言来实现您的新语言的解析器?

Going by your code, you are trying to design a language where classes are kept as a list, in order of declaration, and then you want to be able to instantiate all those classes by iterating the list somehow, and the classes are supposed to be able to know their own index in the list for other uses? 按照您的代码,您正在尝试设计一种语言,在该语言中,类以声明的顺序保存为列表,然后希望通过某种方式迭代列表来实例化所有这些类,并且这些类应该是能够知道列表中其他索引的用途吗?

Is that what you're trying to do? 那是你想做的吗?

In most languages, classes can't be kept in arrays. 在大多数语言中,类不能保存在数组中。 They aren't actual objects. 它们不是实际的对象。 But some, like Smalltalk, Objective-C, Self or TADS, implicitly create an object for each class that contains additional data (C++ has an atrophied form of this called "runtime type-information", or RTTI). 但是有些,例如Smalltalk,Objective-C,Self或TADS,会为每个包含附加数据的类隐式创建一个对象(C ++带有一种萎缩的形式,称为“运行时类型信息”或RTTI)。

If your implementation language doesn't do that, you'll have to do that yourself. 如果您的实现语言没有做到这一点,那么您就必须自己这样做。 Create objects that contain a function that creates an object of the right subclass (and maybe contains some other information like the class name or index in the list so you can find it again), and add that "maker" object to a global list. 创建包含函数的对象,该函数创建正确的子类的对象(并且可能包含一些其他信息,例如列表中的类名或索引,以便您可以再次找到它),然后将该“ maker”对象添加到全局列表中。 Then just use this global list of factory objects to create your instances. 然后,只需使用工厂对象的此全局列表创建实例即可。

You're not saying how you are creating your language though. 您并不是在说如何创建语言。 Are you writing a tool that reads a text file in your language and turns it into a source file in an existing language? 您是否正在编写一种工具,以您的语言读取文本文件并将其转换为现有语言的源文件? Then that tool would just have to create the source code for a few additional classes (like, for class B , it would also create a class B_Maker that is a subclass of class A_Maker , but creates a new object of type B , and then it would create some init() function containing code to crete a new object of each maker and add them to the global list). 然后,该工具将只需要为其他一些类创建源代码(例如,对于class B ,它还将创建一个class B_Makerclass B_Makerclass B_Maker的子class A_Maker ,但是会创建一个类型B的新对象,然后再创建它将创建一些包含代码的init()函数,以创建每个制造商的新对象并将其添加到全局列表中)。

If you're modifying LLVM or some other existing compiler chain, or trying to do this from scratch, the basic approach would be the same (unless there is already an existing object for each class that you can just extend), but you wouldn't be generating source code, but some other data structures that result in the equivalent code being compiled in the end. 如果您要修改LLVM或其他现有的编译器链,或者尝试从头开始执行此操作,则基本方法将是相同的(除非每个类都已经存在一个可以扩展的对象),但是您不会不能生成源代码,但是会导致最终编译等效代码的其他一些数据结构。

Give us more context, and we might be able to help. 给我们更多的环境,我们也许可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM