小编Yan*_*san的帖子

为什么具有上边距的子元素不会拉伸父容器

给定一个具有上边距的子元素的div,为什么边距不在父div内但在外面?

HTML

<div><h1>Titre<h1></div>
Run Code Online (Sandbox Code Playgroud)

CSS

h1 { margin: 20px; }
div { background-color: #DDD; }
Run Code Online (Sandbox Code Playgroud)

cf http://jsfiddle.net/adt515ww/

css

2
推荐指数
1
解决办法
1122
查看次数

如何声明单方法特征

在scala中,有多种方法只使用一种方法声明特征

trait OneMethod extends (A => B)

trait OneMethod {
  def myMethod(a: A) : B
}
Run Code Online (Sandbox Code Playgroud)

每种解决方案的优点和缺点是什么?

scala traits

2
推荐指数
1
解决办法
388
查看次数

是否可以将选项与spark UDF一起使用

我想用Option我的函数的输入类型。

\n\n

udf((oa: Option[String], ob: Option[String])) => \xe2\x80\xa6\n

\n\n

处理null以更实用的方式

\n\n

有没有办法做到这一点 ?

\n

scala apache-spark

2
推荐指数
1
解决办法
1554
查看次数

为什么这个 Scala 代码有一个编译错误类型不匹配

为什么这个 Scala 代码

case class Foo[T]() {
  def bar(tt: T): Unit = ???
  def bar_(s: String)(implicit ev : T =:= String): Unit = bar(s)
}
Run Code Online (Sandbox Code Playgroud)

触发此编译错误

[error] type mismatch;
[error]  found   : s.type (with underlying type String)
[error]  required: T
[error]     def foo2(s: String)(implicit ev: T =:= String) = foo(s)
Run Code Online (Sandbox Code Playgroud)

type-systems scala implicit

2
推荐指数
1
解决办法
70
查看次数

什么是最简单的类型类示例

我想实现最简单的类型类示例.

我来了:我的类型类只是一个常规的scala特征

scala> trait Show[A] { def show(a: A) : String }
defined trait Show
Run Code Online (Sandbox Code Playgroud)

这是类型Int的类型类的实例

scala> implicit val IntShow = new Show[Int] { def show(i: Int) = s"'$i' is an int" }
IntShow: Show[Int] = $anon$1@14459d53
Run Code Online (Sandbox Code Playgroud)

这是一个使用我的类型类的客户端代码

scala> def f[A](a:A)(implicit s : Show[A]) = println(s.show(a))
f: [A](a: A)(implicit s: Show[A])Unit
Run Code Online (Sandbox Code Playgroud)

我们称之为

scala> f(1)
'1' is an int
Run Code Online (Sandbox Code Playgroud)

它会更简单吗?

scala typeclass

1
推荐指数
1
解决办法
185
查看次数

如何解除Either中的部分功能

我想解除一个部分功能Either.

有没有更好的办法 :

def lift[A, B, C](pf : PartialFunction[A, B])(c: A => C) : A =>  Either[C, B] = { a => if (pf.isDefinedAt(a)) Right(pf(a)) else Left(c(a)) }
Run Code Online (Sandbox Code Playgroud)

scala

1
推荐指数
1
解决办法
318
查看次数

如何使用Intellij Idea和Scala

我从事SBT多模块scala项目.我使用Idea来编辑代码和一个带有sbt~compile的外部终端进行编译.

我对这个工作流程不是很满意.

有没有更好的办法 ?

在某些地方有配置选项,doc不是很清楚,所以如何配置:

  • 在Build> Compiler> Scala编译器中
  • 在语言>斯卡拉
  • 在语言> Scala编译服务器

您是否在scala编译器中配置了一些选项?

何时以及如何编译(例如,您是否在编译器设置中自动使用make)?

您是否使用构思或SBT作为增量类型

scala intellij-idea

1
推荐指数
1
解决办法
284
查看次数

用抽象类型定义类型约束

我尝试用抽象类型定义类型约束。但不幸的是,它无法编译。

  sealed trait MatchableValue {
    type A
    def value: A

    def asSingleItemValue : ItemValue
  }

  sealed trait ItemValue {
    type A
    def value: A
  }

  case class StringValue(value: String) extends ItemValue {type A = String}
  case class StringMatchableValue(value: String) extends MatchableValue{
    type A = String
    override def asSingleItemValue =  StringValue(value)
  }
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用

def asSingleItemValue[B <: ItemValue](implicit ev: A =:= B#A) : B
Run Code Online (Sandbox Code Playgroud)

类型约束的目的是在编译时警告此类错误:

  case class IntValue(value: Int) extends ItemValue {type A = Int}
  case class IntMatchableValue(value: Int) extends MatchableValue{
    type …
Run Code Online (Sandbox Code Playgroud)

type-systems scala

1
推荐指数
1
解决办法
275
查看次数

如何将价值提升为未来

有时,我们必须将一个值提升为一个Future.

这有两种方法:

  • 解决方案1: def lift[T](t: T) : Future[T] = Future(t)

  • 解决方案2: def lift[T](t: T) : Future[T] = Future.successful(t)

据我所知,解决方案2似乎更具性能,因为它避免了线程机制和上下文切换.

还有另一个赞成和缺点吗?还有其他解决方案吗?

scala

1
推荐指数
1
解决办法
667
查看次数

How to use literal type in Scala 2.13

I'm trying Literal Types from Scala 2.13 and I encounter the following error :

scala> def double[A <: Singleton] = valueOf[A]
                                           ^
       error: No singleton value available for A.
Run Code Online (Sandbox Code Playgroud)

Could you explain why ?

scala scala-2.13

1
推荐指数
1
解决办法
356
查看次数