Skip to content

Instantly share code, notes, and snippets.

@cryogenian
Created October 5, 2017 14:00
Show Gist options
  • Save cryogenian/6c72316305ed2012d21f0b01ca838445 to your computer and use it in GitHub Desktop.
Save cryogenian/6c72316305ed2012d21f0b01ca838445 to your computer and use it in GitHub Desktop.
class GetLabelType (rl ∷ RowList) (s ∷ Symbol) (a ∷ Type) | rl s → a where
labelType ∷ RLProxy rl → SProxy s → Proxy a
instance getLabelTypeFound ∷ GetLabelType (Cons s a tail) s a where
labelType _ _ = Proxy
else instance getLabelTypeNotFound ∷ GetLabelType (Cons s a tail) s Void where
labelType _ _ = Proxy
else instance getLabelTypeInd ∷ GetLabelType tail ss b ⇒ GetLabelType (Cons s a tail) ss b where
labelType _ _ = labelType (RLProxy ∷ RLProxy tail) (SProxy ∷ SProxy ss)
else instance getLabelTypeVoid ∷ GetLabelType Nil s Void where
labelType _ _ = Proxy
class IsEqual a b where
refl ∷ Proxy a → Proxy b → Boolean
instance isEqualRefl ∷ IsEqual a a where
refl _ _ = true
else instance isEqualFalse ∷ IsEqual a b where
refl _ _ = false
tryGet
∷ ∀ r rl s a out
. RowToList r rl
⇒ GetLabelType rl s out
⇒ IsEqual a out
⇒ IsSymbol s
⇒ SProxy s
→ Proxy a
→ Record r
→ Maybe a
tryGet sproxy aProxy r =
if not refled then Nothing else Just $ RU.unsafeGet (reflectSymbol sproxy) r
where
outProxy ∷ Proxy out
outProxy = Proxy
refled ∷ Boolean
refled = refl aProxy outProxy
main ∷ ∀ e. Eff _ Unit
main = do
DT.traceAnyA $ tryGet (SProxy ∷ SProxy "foo") (Proxy ∷ Proxy Int) {foo: 1}
DT.traceAnyA $ tryGet (SProxy ∷ SProxy "bar") (Proxy ∷ Proxy Int) {foo: 1}
DT.traceAnyA $ tryGet (SProxy ∷ SProxy "foo") (Proxy ∷ Proxy String) {foo: 1, bar: 12}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment