DEV Community

Discussion on: The Coolest Programming Language Features

Collapse
 
aaronpowell profile image
Aaron Powell

I'd like to throw in Pattern Matching from C# 8 and F# as one of the coolest language features.

// fsharp
let name = match person with
           | None -> ""
           | Some p when p.FirstName = "" -> sprintf "Mr/Ms %s" p.LastName
           | Some p -> sprintf "%s %s" p.FirstName p.LastName
// csharp
var name = person switch
           {
               null => ""
               { FirstName: "", LastName: var lastName } => $"Mr/Ms {lastName}"
               { FirstName: var firstName, LastName: var lastName } => $"{firstName} {lastName}"
           };

In both these examples we first check if there was a value (using an Option<T> in F# or a nullable type in C#), then checking if there was or wasn't a first name to determine the name of the person to print.

I totally ❤ using pattern matching in code over nested if blocks!

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

That F# syntax is really pretty! Know anyone who would want to add some new F# snippets to the collection?

TheRenegadeCoder / sample-programs

Sample Programs in Every Programming Language

Sample Programs in Every Language

Build Status Join the chat at https://gitter.im/TheRenegadeCoder/sample-programs

Welcome to the Sample Programs in Every Language repository! What began as a simple 100 Days of Code challenge has expanded into a fun project Within this repository, you'll find a growing collection of simple programs in just about every programming language to date.

Learn More

Originally, I had planned to have an article written for every code snippet in this repository However, in the interest of growing this collection, I removed the pull request merge article requirement As a result, not every code snippet in the repository has an associated article, yet.

Of course, we're adding articles every day, and we're tracking our article progress under the issues tab Feel free to request your favorites from there, and we'll try to move them up the queue. If you can't wait, consider writing the article yourself. Prospective authors can find directions in the contributing doc

Also, I'd be interested in learning more about pattern matching. I've seen it in other functional languages like Racket, but I don't know a lot about it.

Collapse
 
aaronpowell profile image
Aaron Powell

I'll have a look at getting an example up there for you 😊

Collapse
 
saint4eva profile image
saint4eva

I love C#