I was reading chapter 5.2 Type tagging and phantom types of https://books.underscore.io/shapeless-guide/shapeless-guide.html#fn4
and I tried this:
val tmp: FieldType["foo", Int] = "foo" ->> 12
but it gives me error:
type mismatch;
found : Int(12)
required: shapeless.labelled.KeyTag["foo",Int] with Int
see https://scastie.scala-lang.org/f20gxWzcQk21GBv8JDyVog
However, I could pass "foo" ->> 12 val through a parameter type of FieldType[K, V] as:
def getFieldName[K, V](value: FieldType[K, V])(implicit witness: Witness.Aux[K]): K = witness.value
def getFieldValue[K, V](value: FieldType[K, V]): V = value
getFieldName("foo" ->> 12) // gives me foo
getFieldValue("foo" ->> 12) // gives me 12
Can someone explain why the val tmp: FieldType["foo", Int] is not working? It is not working because I am trying to use a phantom type as a regular type?
Another strange fact is
val tmp: 12 = "foo" ->> 12
or
val tmp: Int = "foo" ->> 12
work fine.
I am running this in scala 2.13.8 and shapeless 2.4.0-M1