简体   繁体   English

带有选项类型的 class 参数是否不需要传递值?

[英]Is class parameters with the option type need not to pass a value?

this code:这段代码:

class B(d:Int,val b:Option[Int])

object A extends App  {
  val c=new B(1)
}

is wrong at line B(1) , I have to give a value of b , likeB(1)行是错误的,我必须给出b的值,比如

new B(1,Some(2))

I am studying slick, the example code is:我正在学习 slick,示例代码是:

final class MessageTable1(tag: Tag) extends Table[Message](tag, "message") {

class Table is in file main\scala\slick\relational\RelationalProfile.scala line 119: class 表位于文件 main\scala\slick\relational\RelationalProfile.scala 第 119 行:

abstract class Table[T](_tableTag: Tag, _schemaName: Option[String], _tableName: String) 
  extends AbstractTable[T](_tableTag, _schemaName, _tableName)

So the _schemaName have the type Option[String] , but when the example code create Table , it seems not pass a value to it, only give the other two parameters: tag and "message" , why?所以_schemaName的类型是Option[String] ,但是当示例代码创建Table时,似乎没有给它传递值,只给了其他两个参数: tag"message" ,为什么?

Three lines down , you can see that Table has an auxiliary constructor with two parameters of type Tag and String (note: no Option here): 往下三行,可以看到Table有一个辅助构造函数,带有TagString类型的两个参数(注意:这里没有Option ):

def this(_tableTag: Tag, _tableName: String) = this(_tableTag, None, _tableName)

This is the constructor that is called in the example code you showed, not the primary constructor.这是您显示的示例代码中调用的构造函数,而不是主构造函数。

You can also see the two constructors in the documentation .您还可以在文档中看到这两个构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM