stringf#

ToArray and ToString return (unit -> string), not string. What should I do?


Notes

I'm currently having a problem with the functions "ToString" returning a (unit -> string) type and not a string, and "ToArray" returning (unit -> string[]) and not string[]. Attempting to upcast to string[] or string has no success.

Here is the code:

let readZip filepath = ZipFile.OpenRead filepath
let replaceEnvs str = Environment.ExpandEnvironmentVariables str

let listFiles rawDir =
    replaceEnvs rawDir
    |> Directory.EnumerateFiles

let readModMetadata filepath =
    let archive = readZip filepath
    (archive.GetEntry "mcmod.info").ToString    // Also becomes (unit -> string) and not string

[<EntryPoint>]
let main args =
    let mods = listFiles modsFolder
    let modAsArray = mods.ToArray   // Becomes (unit -> string[]) and not string[]?
    0

Why is this so, and is there a way to get only strings?


Solution

  • So I found it out.

    F# contains the unit type--basically, a filler/"nothing" value, which is always (). Functions with no arguments (or those that intend to be curried but called later) possess a unit argument because, otherwise, they would become value bindings.

    To solve this, the functions need to be called with () passed into them.

    mods.ToArray   // Becomes (unit -> string[]) and not string[], as it expects a unit argument
    mods.ToArray ()// With the unit argument satisfied, we finally get string[]