在一行中编写F#对象表达式 [英] Writing F# object expression in one single line

查看:68
本文介绍了在一行中编写F#对象表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我要为F#编写代码生成器时,我想知道是否可以通过仅生成单行值来避免进入缩进混乱.

As I was about to write a code generator for F#, I was wondering whether I could avoid stepping into the indentation mess by generating only one-line-values.

作为这项工作的一部分,我正在考虑如何在一行中表达对象表达式,但是除了详细模式之外,都无法成功.

As part of this effort, I was considering how I could express Object Expressions in one single line, but couldn't succeed, except in Verbose Mode.

let Expr() = (let ToString = "ToString" in { new System.Object() with member this.ToString() = ToString; member this.GetHashCode() = ToString.GetHashCode()})

问题是我不想在详细模式下生成我的所有代码,这是一项兼容性功能.还有其他选择吗?

The issue is that I don't want to generate all my code in Verbose mode, this is a compatibility feature. Is there any other option?

非常感谢您的见解!
弗朗索瓦(François)

Thanks a lot in advance for your insights!
François

我这样做的原因是我必须在任意表达式中生成对象表达式,并且我想避免计算当前行中的字符数,以计算出我已经拥有多少缩进下一行.

The reason I ask for this is that I have to generate Object Expressions in arbitrary expressions and I would like to avoid to count the number of characters in the current line to compute how much I've to indent the next line.

推荐答案

(无耻的插件)我维护 F#源代码格式化程序,它公开了 API ,以漂亮地打印完整的F#3.0语法.您有几种选择:

(Shameless plug) I maintain F# source code formatter which exposes APIs to pretty-print full F# 3.0 syntax. You have several options:

  • 使用详细模式实现代码生成器,并在输出上运行源代码格式化程序API.然后,您不必担心缩进,并且在详细模式下换行非常容易.

  • Implement your code generator using verbose mode and run the source code formatter APIs on the output. Then you don't have to worry about indentation and line break on verbose mode is very easy.

使用功能组合器实现代码生成器.您可以复制许多组合器并修改. F#缩进规则很明确;您可以在本文中阅读更多内容.

Implement your code generator using functional combinators. There are many combinators in FormatConfig module which you can copy and modify. F# indentation rule is clear; you can read more in this article.

您可能拥有用于漂亮打印的AST.如果您更喜欢轻量级解决方案,则F#编译器CodeDom具有类似的组合器用于代码生成.

You probably have an AST for pretty printing. If you prefer a lightweight solution, F# Compiler CodeDom has similar combinators for code generation.

这篇关于在一行中编写F#对象表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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