DEV Community

Discussion on: First Impressions of F#

Collapse
 
drewknab profile image
Drew Knab • Edited

Addressing your add function first: this is part of the type inference system where F# does Automatic Generalization and Type Inference. The compiler just makes an assumption based on how the function is first used. This is actually very useful when you get into currying and partial application.

If you really want your function to be able to both add two numbers and concatenate strings, you have an option. You can write: let inline add_stuff x y = x + y and that would work. You can read more about Inline Functions, they're pretty useful but can have some downsides depending on how they're used.

I would like to add that just because you can or could do something doesn't necessarily mean you should: add two integers together and return the sum is logically distinct from concatenate two strings and return the combined string. It's (mostly) fine in this trivial example, but the compiler is trying to protect you from writing logically questionable code by yelling at you by default.

As for your question of composition operator vs pipe operator.

Uhhhhhhhhhh, well, they're mostly the same with a distinct difference. I'll refer you F# for Fun and Profit

Collapse
 
dewofyouryouth_43 profile image
Jacob E. Shore

Thanks for your help I'll check this out.

Just to clarify, I don't think it's a good idea to have a function that does both addition and concatenation. My reservation was just that it means you can't just look at the function and immediately know what it's going to do.

Collapse
 
drewknab profile image
Drew Knab

Sure thing.

Gotcha, sure. However, depending on your environment you can see what types the compiler is assigning to your functions. I predominately use Ionide in VS Code, but that extension is available for other text editors as well. Similarly, you get a type signature when you declare a function in fsi.