在scala中创建类型文字,例如AplhaNumericString [英] Creating Type Literals in scala eg. AplhaNumericString

查看:101
本文介绍了在scala中创建类型文字,例如AplhaNumericString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一堆函数来检查字符串是否满足某些条件,例如isValidAlphaNumericString或isValidUserName.它使用我自己的隐式stringOps类.这在处理复杂的要求(例如作者姓名等)时特别有用.要求字符串不具有连续的-_.或空格字符等.使用复杂的正则表达式.但是为什么要停在那里?字符串未正确约束,这意味着某些函数可以将匿名函数映射到该字符串,并在检查完该变量后将其断开.这不是一种非常安全的方法.我想为绑定到特征ConstrainedString的每个约束字符串创建类型,这使我可以指定每种类型如何处理方法.

At the moment I have a bunch of functions that check if strings meet certain circumstances eg isValidAlphaNumericString or isValidUserName. Which uses my own implicit stringOps class. It is quite useful especially when dealing with complex requirements such as author names etc. Requiring string to have only no consecutive -_. or space characters etc. Using complex regex's. But why stop there? The string isn't constrained properly meaning some function could map an anonymous function to it and break the variable after you have checked it. This is not a very safe way to proceed. I want a to create types for each of these constrained strings bound to a trait ConstrainedString which allows me to specify how each type deals with methods.

我正在努力解决设计方面的问题.基本上有两件事需要我帮忙.

I'm struggling with the design side of it. Basically there's two things I need help with.

当函数违反约束时会发生什么?有没有一种方法可以在编译时捕捉到这一点?我是否限制我的类型,使其仅拥有我知道不会破坏约束的方法,即. 2个AlphaNumericString的串联将始终返回字母数字字符串?还是我的实际类型为Option [String],所以如果合同被破坏,则该值将变为None?除了到处引发异常之外,还必须有更好的方法.

What happens when a function breaks the constraint? Is there a way to catch this at compile time? Do I restrict my type from only having methods that I know will not break the constraint ie. Concatenation of 2 AlphaNumericString's will always return alphanumeric String?? Or do I have the actual type as Option[String] so If the contract is broken the value becomes None?? There must be a better way aside from throwing exceptions everywhere.

我认为实现此目标的最佳方法是通过类似于此示例的StringLike类型类

Im presuming the best way to achieve this is through a StringLike typeclass similar to this example http://danielwestheide.com/blog/2013/02/06/the-neophytes-guide-to-scala-part-12-type-classes.html eg

除了我希望能够写

val foo:Bar = "This is a valid Bar"

我认为适当的方法是通过指向Bar apply()方法来提供字符串到Bar的隐式转换,该方法检查该方法是否为有效的Bar.梦想是拥有Numbers在scala中所做的工作,并且无法编译.

I presume the appropriate way is to provide an implicit conversion of string to Bar by pointing to a Bar apply() method which checks if the method is a valid Bar. The dream is to have is work like Numbers do in scala and having this fail to compile.

//fails to compile due to - not being a valid character
val foo:AlphaNumericString = "-Adlsa85464"

如果有人实际上实现了类似id的东西,那么请仔细查看代码:

If anyone has actually implemented something similar id love to look at the code otherwise:

有没有一种简单的方法来保持约束而与功能问题无关

Is there a simple way to maintain the constraint regardless of function issues

我关于如何解决此问题的理论正确吗?

Is my theory on how to approach this issue correct?

推荐答案

它似乎非常适合Scala宏.有关详细信息,请参见此帖子.您可以拥有自己的字符串类型,并对其进行编译时验证.

It seems like a good fit for Scala Macros. See this post for details. You can have your own string types and perform compile time validation on them.

这篇关于在scala中创建类型文字,例如AplhaNumericString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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