[英]SProxy in purescript?
What's the use of Sproxy in purescript?纯脚本中的Sproxy有什么用?
In Pursuit, it's written as在追求中,它被写成
data SProxy (sym :: Symbol)
--| A value-level proxy for a type-level symbol.
and what is meant by Symbol in purescipt?纯符号中的符号是什么意思?
First, please note that PureScript now has polykinds since version 0.14 and most functions now use Proxy
instead of SProxy
.首先,请注意 PureScript 从 0.14 版开始现在有 polykinds,并且大多数函数现在使用Proxy
而不是SProxy
。 Proxy
is basically a generalisation of SProxy
. Proxy
基本上是SProxy
的泛化。
PureScript knows value level strings (known as String
) and type level strings (known as Symbol
). PureScript 知道值级别的字符串(称为String
)和类型级别的字符串(称为Symbol
)。
String
can have any string value at runtime. String
在运行时可以有任何字符串值。 The compiler does not track the value of the string.编译器不跟踪字符串的值。Symbol
is different, it can only have one value (but remember, it is on the type level). Symbol
不同,它只能有一个值(但请记住,它是在类型级别上的)。 The compiler keeps track of this string.编译器会跟踪这个字符串。 This allows the compiler to type check certain expressions.这允许编译器对某些表达式进行类型检查。The most prominent use of Symbols is in records.符号最突出的用途是在记录中。 The difference between a Record and a String-Map is that the compiler knows about the keys at compile time and can typecheck lookups. Record 和 String-Map 之间的区别在于编译器在编译时知道键并且可以进行类型检查查找。
Now, sometimes we need to bridge the gap between these two worlds: The type level and the value level world.现在,有时我们需要弥合这两个世界之间的差距:类型层面和价值层面的世界。 Maybe you know that PureScript records are implemented as JavaScript objects in the official compiler.也许你知道 PureScript 记录在官方编译器中实现为 JavaScript 对象。 This means we need to somehow receive a string value from our symbol.这意味着我们需要以某种方式从我们的符号接收字符串值。 The magical function reflectSymbol
allows us to turn a symbol into a string.神奇的 function reflectSymbol
允许我们将符号转换为字符串。 But a symbol is on the type level.但是符号是在类型级别上的。 This means we can only write a symbol where we can write types (so for example in type definition after ::
).这意味着我们只能在可以编写类型的地方编写符号(例如在::
之后的类型定义中)。 This is where the Proxy hack comes in. The SProxy
is a simple value that "stores" the type by applying it.这就是 Proxy hack 的用武之地SProxy
是一个简单的值,通过应用它来“存储”类型。
For example the get
function from purescript-records
allows us to get a value at a property from a record.例如,从purescript-records
get
function 允许我们从记录中获取属性的值。
get :: forall proxy r r' l a. IsSymbol l => Cons l a r' r => proxy l -> Record r -> a
If we apply the first paramerter we get:如果我们应用第一个参数,我们会得到:
get (Proxy :: Proxy "x") :: forall r a. { x :: a | r } -> a
Now you could argue that you can get the same function by simply writing:现在你可以争辩说你可以通过简单地编写得到相同的 function:
_.x :: forall r a. { x :: a | r } -> a
It has exactly the same type.它具有完全相同的类型。 This leads to one last question:这导致了最后一个问题:
Well, there are certain meta programming szenarios, where you don't programm for a specific symbol, but rather for any symbol.嗯,有一些元编程场景,你不是为特定的符号编程,而是为任何符号编程。 Imagine you want to write a JSON serialiser for any record.想象一下,您想为任何记录编写 JSON 序列化器。 You might want to "iterate" over every property of the record, get
the value, turn the value itself into JSON and then cancatinate the key value pair with all the other keys and values.您可能希望“迭代”记录的每个属性, get
值,将值本身转换为 JSON,然后将键值对与所有其他键和值结合起来。
An example for such an implementation can be found here可以在此处找到此类实现的示例
This is maybe not the most technical explanation of it all, but this is how I understand it.这可能不是最技术性的解释,但这就是我的理解。 Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.