TVSuchty
TVSuchty

Reputation: 133

How to call a function taking no arguments from an instance?

Assume, you have a class:

class AClass a where
  func:: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

How do I call the function func?

Upvotes: 0

Views: 101

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

{-# LANGUAGE AllowAmbiguousTypes, TypeApplications #-}

class AClass a where
  func :: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

foo :: Int
foo = func @SomeTree + func @Double

{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax #-}

bar :: ∀ a . AClass a => a -> Int
bar _ = func @a

Upvotes: 7

Related Questions