Skip to content

Instantly share code, notes, and snippets.

@jlavelle
Created March 27, 2020 02:02
Show Gist options
  • Save jlavelle/1dd9f184c5855bd0b3a315c8748e6d63 to your computer and use it in GitHub Desktop.
Save jlavelle/1dd9f184c5855bd0b3a315c8748e6d63 to your computer and use it in GitHub Desktop.
module Choose where
import Prelude
import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
import Data.Variant (Variant)
import Data.Variant.Internal (VariantRep(..))
import Prim.Row (class Cons)
import Record.Unsafe (unsafeGet)
import Unsafe.Coerce (unsafeCoerce)
data HasField (r :: # Type) = HasField String
hasField :: forall s a r' r. IsSymbol s => Cons s a r' r => SProxy s -> HasField r
hasField s = HasField $ reflectSymbol s
choose :: forall r. HasField r -> Record r -> Variant r
choose (HasField k) r = coerceV $ VariantRep { type: k, value: unsafeGet k r }
where
coerceV :: forall a. VariantRep a -> Variant r
coerceV = unsafeCoerce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment