[英]How do you present any data type to the user with PureScript?
I want to make a very human-friendly development environment, and I'm considering using PureScript to provide the language part. 我想创建一个非常人性化的开发环境,我正在考虑使用PureScript来提供语言部分。 I see that out of the box, Show
doesn't work on records of things which are instances of Show
: 我看到开箱即用, Show
不能用于Show
实例记录:
log (show {a:5})
The 'Try PureScript!' '试试PureScript!' ( http://try.purescript.org/ ) compiler says: ( http://try.purescript.org/ )编译器说:
No type class instance was found for
Prelude.Show { a :: Int
}
Is there a tool for generically printing any data structure, especially one containing records? 是否有用于一般打印任何数据结构的工具,尤其是包含记录的数据结构? Is there some type trickery that would support generically walking over the record to support my own class like present :: Present a => a -> Presentation
? 是否有一些类型的技巧可以支持一般走过记录来支持我自己的类,如present :: Present a => a -> Presentation
? The problem is that I don't know what the types will be ahead of time. 问题是我不知道提前的类型是什么。 The user enters a record and I want to be able to present it. 用户输入记录,我希望能够呈现它。 It seems that I'll have to patch the compiler to support this. 似乎我必须修补编译器以支持这一点。
Records are disallowed in instance heads. 实例头中不允许记录。 For discussion and reasons, see this thread .They must be wrapped in data
or newtype
if we want to write instances for them. 出于讨论和原因,请参阅此主题 。如果我们想为它们编写实例,则必须将它们包装在data
或newtype
。
However, there is a generics library and a deriving mechanism that lets us generate Show
instances. 但是,有一个泛型库和一个派生机制,可以让我们生成Show
实例。
import Data.Generic
data Foo = Foo {a :: Int} | Bar {b :: String}
derive instance genericFoo :: Generic Foo
instance showFoo :: Show Foo where
show = gShow
Working with untyped data in PureScript is done using the purescript-foreign
or the purescript-argonaut
libraries. 在PureScript中使用无类型数据是使用purescript-foreign
或purescript-argonaut
库完成的。 I'd suggest argonaut. 我建议argonaut。
The representation of a record with unknown fields and unknown types for these fields would be: StrMap Json
from the purescript-maps
package. 具有未知字段和未知类型的记录的表示将是: purescript-maps
包中的StrMap Json
。 I'd suggest you take a look at the (not yet merged) documentation over here: https://github.com/hdgarrood/purescript-argonaut-core/blob/565c7e650c51c45570663cf1838ec9cfa307a9c7/README.md . 我建议你看看这里的(尚未合并的)文档: https : //github.com/hdgarrood/purescript-argonaut-core/blob/565c7e650c51c45570663cf1838ec9cfa307a9c7/README.md 。 I've also put together a little example, showing how to match on a heterogeneous array from JavaScript: 我还汇总了一个小例子,展示了如何匹配JavaScript中的异构数组:
-- src/Main.purs
module Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Argonaut (foldJson, Json)
import Data.Foldable (traverse_)
newtype Presentation = Presentation String
unPresentation :: Presentation -> String
unPresentation (Presentation p) = p
instance showPresentation :: Show Presentation where
show = unPresentation
class Present a where
present :: a -> Presentation
instance presentInt :: Present Int where
present = Presentation <<< show
instance presentNumber :: Present Number where
present = Presentation <<< show
instance presentBoolean :: Present Boolean where
present = Presentation <<< show
instance presentString :: Present String where
present = Presentation
presentJson :: Json -> Presentation
presentJson =
foldJson
(const (Presentation "null"))
present
present
present
(const (Presentation "array"))
(const (Presentation "record"))
foreign import vals :: Array Json
main :: forall e. Eff ( console :: CONSOLE | e) Unit
main = traverse_ (log <<< show <<< presentJson) vals
And the corresponding js file: 和相应的js文件:
// src/Main.js
// module Main
exports.vals = [1, 1.2, "hello", true, [1,2,3], {a: 3, b: "hi"}];
Running this program gives you: 运行此程序可以为您提供:
> pulp run
* Building project in/home/creek/Documents/so-christopher-done
* Build successful.
1.0
1.2
hello
true
array
record
Yes, traceAny and related functions from purescript-debug
. 是的,来自purescript-debug
traceAny和相关函数。 Here are a few examples: test/Main.purs#L22 . 以下是一些示例: test / Main.purs#L22 。 I'd post the links to Pursuit, but it doesn't seem to have purescript-debug
at the moment. 我发布了Pursuit的链接,但目前似乎没有purescript-debug
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.