从 LabelledGeneric 实例中提取标签值

编程入门 行业动态 更新时间:2024-10-25 10:21:38
本文介绍了从 LabelledGeneric 实例中提取标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑以下示例:

import shapeless._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo]

现在,labl 的类型是(美化的)

Now, the type of labl is (prettified)

LabelledGeneric[Foo] { type Repr = FieldType[Symbol @@ String("bar"), String] :: FieldType[Symbol @@ String("baz"), Boolean] :: HNil }

它已经传达了我需要的信息,即案例类字段的名称.

which already conveys the information I need, i.e. the names of the case class fields.

我正在寻找的是一种从 labl 到类似

What I'm looking for is a way to go from labl to something along the lines of

"bar" :: "baz" :: HNil

即将包含在单例类型中的信息具体化为一个值.

i.e. materializing the information contained in the singleton types into a value.

这可能吗?我可以使用宏,但我觉得我最终会在 shapeless 中重写与 GenericMacros 对象非常相似的东西,所以我想知道我是否可以直接利用它.

Is this possible? I could use a macro, but I feel like I would end up rewriting something very similar to the GenericMacros object in shapeless, so I'm wondering whether I can leverage it directly.

推荐答案

您可以通过 shapeless.ops.record.Keys 获取记录的键(如 Symbols)代码>.

You can obtain the keys of the record (as Symbols) via shapeless.ops.record.Keys.

这个

import shapeless._ import shapeless.ops.record._ case class Foo(bar: String, baz: Boolean) val labl = LabelledGeneric[Foo] val keys = Keys[labl.Repr].apply println(keys) println(keys.toList.map(_.name))

结果

'bar :: 'baz :: HNil List(bar, baz) : List(String)

更多推荐

从 LabelledGeneric 实例中提取标签值

本文发布于:2023-11-23 21:09:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622868.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   标签   LabelledGeneric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!