Julian
Julian

Reputation: 17

F# error FS0039: The value or constructor 'input' is not defined

let getName name:string = 
    printfn  "the name is %s" name

All im trying to do is make the code display a string, but when I enter for instance "julian" i get the FS0039 erro but when I change it to (name:int) and enter a number it works perfectly fine

Upvotes: 0

Views: 1298

Answers (2)

Romain Deneau
Romain Deneau

Reputation: 3061

let getName name:string = means getName is a function returning a string and taking a parameter name with a type to be inferred - its temporary signature is (name: 'a) -> string. What is misleading is the whitespace: it's not because there is no whitespace here name:string that :string refers to the parameter name rather than the function getName.

Based on its body printfn "the name is %s" name :

  • The function should return unit
  • The parameter name has type string (see %s placeholder)

So, you can :

  • either add parentheses let getName (name: string) = ...
  • or (better) drop the type annotation let getName name = to rely on the type inference that is working here due to %s.

One last point: your function is called "get something" but actually returns nothing. It's inconsistent and makes it harder to use and maintain. Something like greet, print, printName is more appropriate.

Upvotes: 1

Gus
Gus

Reputation: 26174

I think you're missing parens around name:string, because otherwise : string is parsed as the return value fo the whole function, and if your function just perform side-effects like printing, the return value is unit.

Upvotes: 2

Related Questions