Giter VIP home page Giter VIP logo

Comments (7)

ceedubs avatar ceedubs commented on May 12, 2024

@milessabin how did you envision this being implemented? I assumed the implementation would check for universal equality with the value of the Witness of the singleton type, but it doesn't look like that works. assertEquals(Witness(0).narrow, 0) results in expected: <shapeless.SingletonTypesTests$fresh$macro$3$1@60aff886> but was:<0>. Am I thinking about this wrong?

from shapeless.

milessabin avatar milessabin commented on May 12, 2024

@ceedubs Umm ... did you mean Witness(0).value?

from shapeless.

ceedubs avatar ceedubs commented on May 12, 2024

@milessabin ah yes that's what I meant. That makes my assertEquals example pass.

Unfortunately, I still can't get my implementation to work. It seems like it should be straightforward so I'm probably doing something silly. Maybe I'll let someone else take this one :)

from shapeless.

milessabin avatar milessabin commented on May 12, 2024

Do you have a branch or a gist we could take a look at?

from shapeless.

ceedubs avatar ceedubs commented on May 12, 2024

To be honest I don't have a firm understanding of singletons and witnesses; they still seem like magic to me. So I'm probably doing something quite foolish, but with risk of embarrassing myself, here it goes.

I had added this to SingletonOps:

def extract[V >: T](v: V): Option[T] = if (v == witness.value) Some(witness.value) else None

However, I haven't succeeded in getting this to return anything other than None. Also type refinements seem to be causing trouble when trying to check that the return type matches an Option of the singleton type.

from shapeless.

milessabin avatar milessabin commented on May 12, 2024

The following works for me,

scala> def cast(w: Witness)(x: Any): Option[w.T] = if(x == w.value) Some(w.value) else None
cast: (w: shapeless.Witness)(x: Any)Option[w.T]

scala> cast(0)(1: Any)
res0: Option[Int(0)] = None

scala> cast(0)(0: Any)
res1: Option[Int(0)] = Some(0)

I was thinking that we should have Typeable instances for types with an available implicit Witness, and then some additional typeable syntax that adds something like foo.cast(<literal>) where the literal singleton type is extracted from the inferred Witness as in cast above.

from shapeless.

milessabin avatar milessabin commented on May 12, 2024

Fixed in 7eb430b.

from shapeless.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.