Skip to content

Instantly share code, notes, and snippets.

View nicolasstucki's full-sized avatar
🌴
On vacation

Nicolas Stucki nicolasstucki

🌴
On vacation
  • LAMP / EPFL
  • Switzerland
View GitHub Profile
@nicolasstucki
nicolasstucki / diff.md
Last active July 11, 2016 13:43
Reversi diff 2.12.0-M4 vs 2.12.0-M5
--- reversi-fastopt-M4.js	2016-07-11 15:10:35.000000000 +0200
+++ reversi-fastopt-M5.js	2016-07-11 14:38:25.000000000 +0200
@@ -1351,18 +1351,6 @@
     }
   }
 }
-function $f_scm_FlatHashTable$HashUtils__elemToEntry__O__O($thiz, elem) {
-  return ((elem === null) ? $m_scm_FlatHashTable$NullSentinel$() : elem)
-}

Stoic functions implementation

Support for stoic functions

@stoic and Stoical
trait Stoical
class stoic extends scala.annotation.StaticAnnotation
@nicolasstucki
nicolasstucki / after-pickling.txt
Created July 18, 2016 09:47
Output for #1355.
[cannot display due to scala.MatchError: 98 (of class java.lang.Integer), raw string = PackageDef(Ident(util),List(Import(Select(Ident(scala),annotation),List(Ident(tailrec))), Import(Ident(difflib),List(Ident(_))), ValDef(DiffUtil,TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,dotc)),util),DiffUtil$)],Apply(Select(New(TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,dotc)),util),DiffUtil$)]),<init>),List())), TypeDef(DiffUtil$,Template(DefDef(<init>,List(),List(List()),TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,<root>)),scala),Unit)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,java)),lang),Object)]),<init>),List())),ValDef(_,TypeTree[TermRef(TermRef(ThisType(TypeRef(NoPrefix,dotc)),util),DiffUtil)],EmptyTree),List(ValDef(ANSI_DEFAULT,TypeTree[ConstantType(Constant())],Literal(Constant())), ValDef(ANSI_RED,TypeTree[ConstantType(Constant())],Literal(Constant())), ValDef(ANSI_GREEN,TypeTree[ConstantType(Constant())],Literal(Constant())), ValDef(ANSI_YELLOW,T
[info] Test dotc.tests.compileStdLib started
assertion failure for X <:< X, frozen = true
assertion failure for X <:< X, frozen = true
assertion failure for  = X <:<  = X, frozen = true
assertion failure for scala.collection.Set[X] <:< collection.Set[X], frozen = true
assertion failure for X <:< X, frozen = true
assertion failure for X <:< X, frozen = true
assertion failure for X <:< X, frozen = true
assertion failure for  = X <:<  = X, frozen = true

Phantom types

Phantom types are designed to support compile time type evidences without any overhead costs to runtime. Phantom evidences are usually in the form of implicit arguments, which once resolved, can be erased by the compiler.

Because of these properties, phantom types are completely outside of the normal type lattice, and as such these phantom types do not inherit the semantics of Any.

val badType = RefinedType(RefinedType(TypeRef(RefinedType(RefinedType(RefinedType(RefinedType(TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,immutable)),Nil$),$anon), scala$collection$TraversableOnce$$A, TypeAlias(TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,immutable)),Nil$),scala$collection$LinearSeqLike$$A), 1)), scala$collection$GenTraversableOnce$$A, TypeAlias(TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,immutable)),Nil$),scala$collection$LinearSeqLike$$A), 1)), scala$collection$Iterator$$A, TypeAlias(TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,immutable)),Nil$),scala$collection$LinearSeqLike$$A), 1)), scala$collection$AbstractIterator$$A, TypeAlias(TypeRef(TypeRef(ThisType(TypeRef(NoPrefix,immutable)),Nil$),scala$collection$LinearSeqLike$$A), 1)),$anon), scala$collection$TraversableOnce$$A, TypeAlias(RefinedType(RefinedType(TypeRef(ThisType(TypeRef(NoPrefix,scala)),Tuple2), scala$Tuple2$$T1, TypeAlias(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,scala)),Predef),String), 1)), scala$Tuple2$$T2, TypeAlias(TypeRef

With fingerprints

[info] Test run started
[info] Test dotty.tools.dotc.CompilationTests.runAll started
[=======================================] completed (775/775, 38s)  
[info] Test dotty.tools.dotc.CompilationTests.compileNeg started
[=======================================] completed (252/252, 2s)  
[info] Test dotty.tools.dotc.CompilationTests.compilePos started
[=======================================] completed (871/871, 59s)  
[info] Test dotty.tools.dotc.CompilationTests.testPickling started
import scala.annotation.implicitNotFound
import scala.reflect.ClassTag

object Test {

  import Phantom_=::=._
  //  import Normal_=::=._

  def main(args: Array[String]): Unit = {

Example implementation: safe type constraint =:=

In this example we will see how to reimplement =:= using phantom types and in the process fix a vulnerability that it has. It is a simplified version of TypeConstraint which also implements <:<.

=:= in Scala 2.x

Type constraints =:= gives an evidence that two generic types are in fact equal and provides a mechanism to safely cast one type to the other. In the following example we want to assign an x: X to an ys: Array[Y] where we don't know if it is possible (this will be known at call site). Therefore we use an implicit evidence ev: X =:= Y to know that they are equal and ev(x) to cast x form X to Y.

I: File: out/linkCustomLib/custom-lib/Map2.class
I: Done in 0.118 ms, nt size: 54
I: public interface abstract class Map2#2
* Version: 52.0 (supported by JDK 1.8)
* Constant Pool [34]
#0 [null entry]: -
#1 [Utf8]: Map2
#2 [Class]: Map2#1
#3 [Utf8]: java/lang/Object
#4 [Class]: java/lang/Object#3