可以在scala中命名常量吗? [英] Possible to have named constants in scala?

查看:43
本文介绍了可以在scala中命名常量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来注解需要 Java 中的常量.我想做:

It looks like annotations require constants in Java. I'd like to do:

object ConfigStatics {
  final val componentsToScan = Array("com.example")
}

@PropertySource( ConfigStatics.componentsToScan )   // error: constant value required
class MyConfig extends WebMvcConfigurerAdapter {
}

哪里

@PropertySource( Array("com.example") ) 
class MyConfig extends WebMvcConfigurerAdapter {
}

没问题.

遗憾的是,scala 无法将静态最终 val 识别为常量值.

Sadly, scala doesn't recognize a static final val as a constant value.

这里有什么可做的吗,或者在 Scala 中根本不可能有命名常量?

Is there's anything to do here, or is it simply not possible to have named constants in scala?

推荐答案

这看起来像是一个错误.

This looks like a bug.

SLS 6.24 说文字数组 Array(c1, c2, ...) 是一个常量表达式.

SLS 6.24 says a literal array Array(c1, c2, ...) is a constant expression.

SLS 4.1 说一个常量值定义 final val x = e 意味着 x 被替换为 e.

SLS 4.1 says a constant value definition final val x = e means x is replaced by e.

它不能那样工作,所以要么是规范错误,要么是实现错误.

It doesn't work that way, so either it's a spec bug or an implementation bug.

  final val j = Array(1,2,3)
  def k = j  // j
  final val x = 3
  def y = x  // 3

这是这个问题的副本,retronym 承诺会就此开票.

This is a duplicate of this question where retronym promised to open a ticket about it.

那是三年前的事了.我想知道他的终端上是否还有黄色的便利贴?

That was three years ago. I wonder if there's still a yellow post-it on his terminal?

这篇关于可以在scala中命名常量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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