是否有可能定义依赖于彼此并在分离的文件中定义的类型? [英] Is it possible to define types that depend on each other and are defined in separated files?

查看:124
本文介绍了是否有可能定义依赖于彼此并在分离的文件中定义的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现与扩展的解析功能库。我决定,我会用fsyacc,因为我从大学就知道了。不幸的是我遇到了以下问题。

I am trying to implement a library with extended parsing capabilities. I decided that I will use fsyacc because I knew it from the university. Unfortunately I encountered following problem.

我定义一个类为我的语法的头部(),并将其在单个文件执行。然后,我定义解析器:

I defined a class for the head of my grammar (Head) and place its implementation in a single file. Then I defined parser as:

...
%start head
%type <Head> head
...

Fsyacc产生seeparated模块(解析器)。为了取得成功,就必须在下列顺序编译: Head.fs Parser.fs

Fsyacc generates seeparated module (Parser). In order to succeed it has to be compiled in following order: Head.fs Parser.fs

为了使这个库类似于您可以在.NET中找到,我想一个静态的解析方法添加到。不幸的是,我需要使用方法从解析器模块。

In order to make this library similar to what you can find in .NET I would like to add a static Parse method to Head. Unfortunately I would need to make use of methods from Parser module.

我知道,这样的类型的依赖关系可以解决与操作符,但它仅适用于在一个文件中定义的类型。

I know that such type dependencies can be solved with 'and' operator but it is only applicable to types defined in one file.

是否有任何其他的方法来创建依赖于对方,甚至当他们在不同的文件?我一直在寻找像在C / C申报/实施分离机制++的类型,但我couldn 找不到任何东西。

Is there any other way to create types that depend on each other even when they are in separate files? I was looking for declaration/implementation separation mechanism like the one in C/C++ but I couldn't find anything.

推荐答案

简短的回答:没有。有没有办法做到在多个文件相互递归实体在F#2.0。 (这是我们计划中的语言的下一个版本,以解决一些事情。)

Short answer: no. There's no way to do mutually recursive entities across multiple files in F# 2.0. (This is something we plan to address in the next version of the language.)

您可以解决此通过各种方式,通常使用一个点间接和突变。例如,你的头型可以有一个静态的InitializeParser方法戳一个函数值到可变的全局变量,然后在头中定义的静态解析的方法可以通过该可变的全球呼叫,解析器后居然被定义,它可以去调用InitializeParser捅值。(如果这没有任何意义,我能拼出来的更多细节。)

You can work around this in a variety of ways, typically using a point of indirection and mutation. For example, your Head type could have a static 'InitializeParser' method that pokes a function value into a mutable global variable, and then the static Parse method defined in Head could call via that mutable global, and after the parser is actually defined, it can go and call InitializeParser to poke the value in. (If that doesn't make sense, I can spell it out in more detail.)

这篇关于是否有可能定义依赖于彼此并在分离的文件中定义的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆