简体   繁体   中英

Using a type of a parameterized data type in a Haskell function

Let's say I have some algebraic data type in Haskell:

data Foo a = ...

I'd like to have a function that could "extract" this type a and do something with it, assuming it satisfies certain conditions. Specifically, I need something like:

fun :: Bounded a => Foo a -> a

with intended usage as follows:

fun foo = maxBound :: a

Of course this notation is incorrect in Haskell, but I think my intentions are clear. Is it possible to do something like this?

You don't need anything, it just works.

fun :: Bounded a => Foo a -> a
fun _ = maxBound

The compiler knows that the result of fun would be an a and therefore will call the correct maxBound .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM