New user of F#
-
Thursday, July 12, 2012 3:04 AM
Hello All,
I don't know what is wrong here.
I'm trying to understand the link between 2 files.
I don't get a simple string to show.
Thanks in advance
Good.fs
Module Good
Let message =
"Hello World of FSharp"
Programs.fs
namespace Good
let mes = message()
[<EntryPoint>]
let main () =
printf mes
0
Newbie
All Replies
-
Thursday, July 12, 2012 5:01 AM
First: please make sure to insert the code with "Insert Code Block" - yours is misformated and as it is wrong ("Let", "Module", ...) - I guess Word or whatever took over and uppercased your stuff here.
Concerning the problem: I think the problem is that you try to invoke "message" with () while it is only a constant/value. And of course you do not give printf any formating to work upon - make sure to look at the documentation for printf
This one should not even compile - try this:
let mes = Good.message printfn "%s" mes
-
Friday, July 13, 2012 12:33 AM
module Good let message = "Hello World of FSharp" // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. namespace Good [<EntryPoint>] let main = let Test = Good.message sprintf "%s" Test 0
Still not working. Thanks to reply.
When I will get this I think I will understand the basic and will be able to go further with that new programming language.
Newbie
-
Friday, July 13, 2012 4:20 AM
look carefully: now you used sprintf instead of printf(n) - the s is there to give you a string back, this is similar to String.Format - nothing will be print to screen/console (this is missleading I guess) - the n at the end will indicate that you want a newline-character at the end of your output (string or console).
You should see a very good compiler-error for this, and I think you will only understand the basics if you learn to read those compiler-errors and the types intellisense provide.
-
Friday, July 13, 2012 12:22 PM
I tried both of them,
The error I'm getting is
Namespaces cannot contain values. Consider using module to hold your value declarations.
The section underline is let main
I looked over the net and there is nothing explaining something close to what I achieve.
Thanks.
Newbie
-
Friday, July 13, 2012 1:29 PM
According to MSDN doc your compiler is right "Namespaces cannot directly contain values and functions. Instead, values and functions must be included in modules, and modules are included in namespaces. Namespaces can contain types, modules." http://msdn.microsoft.com/en-us/library/dd233219.aspx
So working should look like this:
namespace Some module Good = let message = "Hello World of FSharp" // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. [<EntryPoint>] let main args = let Test = message printfn "%s" Test 0
Note that signature of main function should include parameter for command line arguments (args). And compiler will throw an error if both namespace and module will have the same name. So I gave name 'Some' to namespace above.
Petr
- Edited by Plepilov Friday, July 13, 2012 1:30 PM
- Marked As Answer by Dan Quirk - MSFTMicrosoft Employee, Owner Friday, July 13, 2012 9:50 PM
- Unmarked As Answer by Martin P Saturday, July 14, 2012 1:10 PM
-
Saturday, July 14, 2012 1:15 PM
I tested your explanation.
I create a file Program.fs that is the main one.
// Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. namespace Sharp // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. [<EntryPoint>] let main args = let Test = message printfn "%s" Test 0
Then I create the other files that I link to it. Good.fs
module Sharp let message = "Hello World of FSharp"
With that create I'm getting this error.
Namespaces cannot contain values. Consider using a module to hold your value declarations.
The part underline is "main args"
Newbie
- Edited by Martin P Saturday, July 14, 2012 1:16 PM
-
Saturday, July 14, 2012 2:38 PM
In your Program.fs file you have namespace 'Sharp'
According to F# syntax, namespaces cannot directly contain values and functions, but you declared in your namespace function 'main'.
You should either not declare namespace at all or declare a module inside this namespace which would contain 'main' function declaration.
Petr
-
Saturday, July 14, 2012 4:47 PM
Sorry I'm not quite understanding.
When you create a windows application you always pass value to other screen and most of the time they are in different files.
Why I cannot pass the value to the main and print them.
Thanks.
Newbie
-
Saturday, July 14, 2012 6:20 PM
You can use this for example:
1) File Good.fs
module Sharp let message = "Hello World of FSharp"
2) File Program.fs
namespace Some module Good = // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. [<EntryPoint>] let main args = let Test = Sharp.message printfn "%s" Test 0
Note the using of qualified name Sharp.message from module Sharp (file Good.fs)
Or you can use 'open' directive to use 'message' value without module qualifier:
3) File Program.fs
namespace Some open Sharp module Good = // Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. [<EntryPoint>] let main args = let Test = message printfn "%s" Test 0
Make sure that file Good.fs is placed in the project tree in Visual Studio above the file Program.fs. If not - select the file in the Solution Explorer and use context menu items "Move Up" and "Move Down".
Petr
- Marked As Answer by Martin P Sunday, July 15, 2012 1:02 AM
-
Friday, August 17, 2012 1:34 AM
I have a related problem. In Visual Studio I create a project with 4 modules, listed in System Explorer as:
Third.fs
Second.fs
First.fs
Program.fs
In Program.fs, I type 'open First' and that compiles fine.
In First.fs, I type 'open Second' and get 'the namespace or module 'Second' is not defined.'
I must be overlooking something simple -- surely it is possible to call modules from somewhere other than 'Program.fs'?
EDIT: When I run it under FSI, it works ok, so it must be a bug of some kind I guess.
- Edited by David X Lee Friday, August 17, 2012 4:10 AM

