structural-typing

Getting a structural type with an anonymous class's methods from a macro

馋奶兔 提交于 2019-11-28 03:30:27
Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro system in 2.10.0, and the type member part is extremely easy: object MacroExample extends ReflectionUtils { import scala.language.experimental.macros import scala.reflect.macros.Context def foo(name: String): Any = macro foo_impl def foo_impl(c: Context)(name: c.Expr[String]) = { import c.universe._ val Literal(Constant(lit: String)) = name.tree val

Whats the difference between using a class and interface?

ぃ、小莉子 提交于 2019-11-27 09:18:21
What is the difference between doing this export class Comment { likes: string; comment: string; constructor(likes: string, comment: string){ this.comment = comment; this.likes = likes; } } and this export interface CommentInterface { likes: string; comment: string; } in relation to declaring an observable type register: Observable<CommentInterface[]> { return this.http.get() } As JB Nizet quite correctly points out, the deserialized JSON values that result from HTTP requests will never be instances of a class. While the dual role (see below) of the class construct in TypeScript makes it

Getting a structural type with an anonymous class's methods from a macro

陌路散爱 提交于 2019-11-27 05:08:59
问题 Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro system in 2.10.0, and the type member part is extremely easy: object MacroExample extends ReflectionUtils { import scala.language.experimental.macros import scala.reflect.macros.Context def foo(name: String): Any = macro foo_impl def foo_impl(c:

Pattern matching structural types in Scala

我与影子孤独终老i 提交于 2019-11-27 01:49:09
问题 Why does this print wtf? Does pattern matching not work on structural types? "hello" match { case s: { def doesNotExist(i: Int, x: List[_]): Double } => println("wtf?") case _ => println("okie dokie") } 回答1: Running this example in the Scala interpreter with unchecked warnings on ( scala -unchecked ) produces the following warning: warning: refinement AnyRef{def doesNotExist(Int,List[_]): Double} in type pattern is unchecked since it is eliminated by erasure . Unfortunately, a generic type

Scala - how to define a structural type that refers to itself?

别等时光非礼了梦想. 提交于 2019-11-27 01:41:47
问题 I'm trying to write a generic interpolate method that works on any type that has two methods, a * and a + , like this: trait Container { type V = { def *(t: Double): V def +(v: V): V } def interpolate(t: Double, a: V, b: V): V = a * (1.0 - t) + b * t } This doesn't work though (on Scala 2.8.0.RC7), I get the following error messages: <console>:8: error: recursive method + needs result type def +(v: V): V ^ <console>:7: error: recursive method * needs result type def *(t: Double): V ^ How do I

Whats the difference between using a class and interface?

放肆的年华 提交于 2019-11-26 14:38:28
问题 What is the difference between doing this export class Comment { likes: string; comment: string; constructor(likes: string, comment: string){ this.comment = comment; this.likes = likes; } } and this export interface CommentInterface { likes: string; comment: string; } in relation to declaring an observable type register: Observable<CommentInterface[]> { return this.http.get() } 回答1: As JB Nizet quite correctly points out, the deserialized JSON values that result from HTTP requests will never