Smalltalk / Squeak消息处理方法(Smalltalk / Squeak message handling method)
一个非常简短的问题 - 如何命名消息处理方法(在消息传递时调用的第一个方法),以及在哪个类中定义它?
A pretty short question - how is the message handling method named (the first method that is called on message passing), and in which class it is defined?
原文:
最满意答案
要回答这个问题,知道什么是准引用是有帮助的。 从GHC文档中 ,准引号是一个值
data QuasiQuoter = QuasiQuoter { quoteExp :: String -> Q Exp, quotePat :: String -> Q Pat, quoteType :: String -> Q Type, quoteDec :: String -> Q [Dec] }
也就是说,它是从任意String到
ExpQ
,PatQ
,TypeQ
和DecQ
一个或多个的DecQ
,它们分别是表达式,模式,类型和声明的模板Haskell表示。当您使用准引号时,GHC将解析器应用于String以创建
ExpQ
(或其他类型),然后在生成的模板haskell表达式中拼接以生成实际值。听起来你要做的就是将quasiquote解析和拼接分开,这样你就可以访问TH表达式了。 然后您可以将该表达式导入另一个模块并自己将其拼接在那里。
知道准引物的类型,很明显这是可能的。 通常你使用QQ作为
-- file Expr.hs eval :: Expr -> Integer expr = QuasiQuoter { quoteExp = parseExprExp, quotePat = parseExprPat } -- file Foo.hs import Expr myInt = eval [expr|1 + 2|]
相反,您可以自己提取解析器,获取TH表达式,然后将其拼接:
-- file Foo.hs import Expr -- run the QQ parser myInt_TH :: ExpQ myInt_TH = quoteExp expr "1 + 2" -- file Bar.hs import Foo.hs -- run the TH splice myInt = $(myInt_TH)
当然,如果您自己编写所有这些内容,则可以跳过准引号并直接使用解析器和模板Haskell。 这两种方式几乎都是一样的。
To answer this, it's helpful to know what a quasi-quoter is. From the GHC Documentation, a quasi-quoter is a value of
data QuasiQuoter = QuasiQuoter { quoteExp :: String -> Q Exp, quotePat :: String -> Q Pat, quoteType :: String -> Q Type, quoteDec :: String -> Q [Dec] }
That is, it's a parser from an arbitrary String to one or more of
ExpQ
,PatQ
,TypeQ
, andDecQ
, which are Template Haskell representations of expressions, patterns, types, and declarations respectively.When you use a quasi-quote, GHC applies the parser to the String to create a
ExpQ
(or other type), then splices in the resulting template haskell expression to produce an actual value.It sounds like what you're asking to do is separate the quasiquote parsing and splicing, so that you have access to the TH expression. Then you can import that expression into another module and splice it there yourself.
Knowing the type of a quasi-quoter, it's readily apparent this is possible. Normally you use a QQ as
-- file Expr.hs eval :: Expr -> Integer expr = QuasiQuoter { quoteExp = parseExprExp, quotePat = parseExprPat } -- file Foo.hs import Expr myInt = eval [expr|1 + 2|]
Instead, you can extract the parser yourself, get a TH expression, and splice it later:
-- file Foo.hs import Expr -- run the QQ parser myInt_TH :: ExpQ myInt_TH = quoteExp expr "1 + 2" -- file Bar.hs import Foo.hs -- run the TH splice myInt = $(myInt_TH)
Of course if you're writing all this yourself, you can skip the quasi-quotes and use a parser and Template Haskell directly. It's pretty much the same thing either way.
相关问答
更多-
原则上, wx-config绝对应该能够在同一个库的多个版本之间进行选择(这大约是其整个逻辑和目的的90%),但是恐怕wx-config逻辑的构建选择和 - --host选项,这将需要选择正确的构建在你的情况。 我不确定自己,因为我实际上从不使用wx-config在多个构建中进行选择,而是根本不安装它们并使用构建目录中的脚本。 即我确定的工作是: $ mkdir -p ~/build/wx/gtk $ cd $_ $ ~/src/wx/configure # of course, you can add o ...
-
下面是从“学习你一个好主意的Haskell!”的Haskell模块介绍: http://learnyouahaskell.com/modules 我会将这个包称为Haskell软件的最小可交付单元。 对于大多数人来说,“包是Hackage上的东西”就足够了 。 如果你正在发布一个可执行文件,我不会担心你的项目分成多个软件包,直到你开始第二个项目,并且希望从第一个开始重新使用模块。 Here is an introduction Haskell modules from "Learn You a Haskel ...
-
Google Places API限制(Google Places API Limits)[2021-12-27]
就关键而言,它仅适用于Places网络服务: https : //developers.google.com/maps/documentation/places/ 您的文档链接指向Places库 - 这是JavaScript API v3的一项服务。 这里不需要一个关键字,但是当您加载Maps JS( https://developers.google.com/maps/documentation/javascript/tutorial#HelloWorld )时,您可以通过添加&key={yourkey} ... -
了解haskell代码(Understanding haskell code)[2022-04-10]
-- Filter a list... filter -- ...of nested tuples where the first element of the second element of the tuple... (\(_, (variable1, _)) -> -- ...is not an element of [something1, something2] variable1 `notElem` [something1, something2]) (_, (varible1, _) ... -
您可以将其转换为PrintfArg类型,以便以所需的格式打印它,例如 printf "%.3f" (realToFrac delta :: Double) 可以。 You can convert it to a PrintfArg type to print it in the desired format, e.g. printf "%.3f" (realToFrac delta :: Double) does it.
-
我需要一种方式来运行Haskell代码,而无需在Mac上进行任何安装(I need a way to run Haskell code without any install on a Mac)[2023-04-19]
你只需要安装自制软件,如果你在主目录下这样做,你可以不使用root权限。 然后,您可以brew install cabal-install ,它将自动安装cabal和ghc,或者brew install stack以安装堆栈。 You just need to install homebrew, which you can do without root rights if you do so in your home directory. You can then brew install cabal-i ... -
涉及`std :: string`的编译会导致Windows错误状态(Compilations involving `std::string` cause Windows error status)[2021-01-31]
GetLastError()返回由此线程调用的WinAPI函数设置的最后一个错误代码。 请注意,WinAPI函数在成功时不一定会设置代码: 大多数函数只有在失败时才会调用SetLastError或SetLastErrorEx。 您尚未调用失败的WinAPI函数。 因此,错误代码是不确定的。 它或者是未初始化的(意思是未定义的行为),或者是由未知函数设置的(只是稍微没有意义)。 标准库使用异常来表示错误,而不是Windows API。 “这是行为吗?” 预期这种行为意味着任何值都可以接受,因为该函数没有在正确 ... -
除非已经使用GHC -split-objs选项进行编译,否则库模块将作为一个整体进行链接。 但是这会使编译速度变慢,所以它不是默认值。 从GHC文档 : 然而,分别组装所有部分是昂贵的,所以这比编译通常慢。 我们使用这个特性来构建GHC的库(警告:除非你知道你在做什么,否则不要使用它)。 A library module is linked as a whole unless it has been compiled with the GHC -split-objs option. However this ...
-
Haskell流媒体下载(Haskell streaming download)[2022-03-24]
您可能正在从最新版本的http-conduit寻找httpSource 。 它的行为与Python的请求非常相似:您可以获得大量的块。 保存到文件 这很简单,只需将源直接重定向到文件接收器即可。 #!/usr/bin/env stack {- stack --install-ghc --resolver nightly-2016-11-26 runghc --package http-conduit -} {-# LANGUAGE OverloadedStrings #-} import Network. ... -
如何在Haskell编译期间将代码包含在不同的地方?(How to include code in different places during compilations in Haskell?)[2023-07-02]
要回答这个问题,知道什么是准引用是有帮助的。 从GHC文档中 ,准引号是一个值 data QuasiQuoter = QuasiQuoter { quoteExp :: String -> Q Exp, quotePat :: String -> Q Pat, quoteType :: String -> Q Type, ...