haskus / packages

Haskus packages
https://haskus.org/
24 stars 11 forks source link

Size of the Variant type? #17

Closed wpoosanguansit closed 5 years ago

wpoosanguansit commented 5 years ago

Hi,

I am trying out the Variant code and I am curious if there is a way to check the size of the Variant types, not the size of the value.

for v :: V '[A, B, C, D] I would like to to get the size of 4 out. Thank you for your help.

hsyl20 commented 5 years ago

Hi,

You can do it as follows:

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}

import Haskus.Utils.Variant
import Haskus.Utils.Types
import Haskus.Utils.Types.List

variantSize :: forall xs. (KnownNat (Length xs)) => V xs -> Word
variantSize _ = natValue @(Length xs)

Test:

> data A
> data B
> data C
> data D
> let v = undefined :: V '[A,B,C,D]
> variantSize v
4
wpoosanguansit commented 5 years ago

Thank you.

On Jan 8, 2019, at 9:55 AM, Sylvain Henry notifications@github.com wrote:

Hi,

You can do it as follows:

{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-}

import Haskus.Utils.Variant import Haskus.Utils.Types import Haskus.Utils.Types.List

variantSize :: forall xs. (KnownNat (Length xs)) => V xs -> Word variantSize _ = natValue @(Length xs) Test:

data A data B data C data D let v = undefined :: V '[A,B,C,D] variantSize v 4 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

hsyl20 commented 5 years ago

You're welcome. As it can be useful to others, I've added it for the next release.