2

I was trying to work out some simple examples of type-level programming in Scala 2.11. Here is a function to tell type equality:

def type_==[A, B](implicit ev: A =:= B = null) = ev != null 

Here =:= is defined in the prelude, but for our purpose, even a simple definition such as

class =:=[A, B] implicit def equalTypeInstance[A] = new =:=[A, A] 

would do. To be sure, can do

type_==[Int, String] // false type_==[Int, Int] // true 

Next, I encode booleans as types - to make things simple, I avoid to define any operation

sealed trait Bool trait True extends Bool trait False extends Bool 

I can again check that

type_==[True, True] // true 

So I thought I might convert Bool to Boolean by doing

def bool2boolean[A <: Bool] = type_==[A, True] 

Here's the catch:

bool2boolean[True] // false 

Can anyone explain the reason why?

1 Answer 1

3

Implicits don't magically pass through functions - each function creates its own scope, and the implicit resolution happens there:

def bool2boolean[A <: Bool] = type_==[A, True] 

In this scope the compiler attempts to resolve a =:=[A, True], can't find one, and so this function always returns False.

Try passing the evidence through this function:

def bool2boolean[A <: Bool](implicit ev: A =:= True = null) = type_==[A, True] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.