David
David

Reputation: 6152

How do I declare the type of a variable inside a function in Haskell?

This is a reframe of the question that was asked at Defining variables inside a function Haskell

I have a function the beginning of which looks like this:

recursiveLs :: FilePath -> IO [FilePath]
recursiveLs dir =
   do
       folderExists <- doesDirectoryExist dir
       if folderExists
          then ...

The question is, how can I explicitly declare the type of folderExists before I assign to it in the action?

Upvotes: 3

Views: 2633

Answers (1)

Well, let's try to do what you want in ghci:

> (a :: Integer) <- return 10

<interactive>:2:7:
    Illegal type signature: `Integer'
      Perhaps you intended to use -XScopedTypeVariables
    In a pattern type-signature

So, we should enable that pragma.

> :set -XScopedTypeVariables

And try again

> (a :: Integer) <- return 10
a :: Integer

Now we have a equal to 10, which is Integer:

> a
10
it :: Integer

Also, I believe that you've forgot about = in your recursiveLs function, there should be something like recursiveLs dir = do ...

Upvotes: 7

Related Questions