News for hackers. Stuff that matters.
Re:Why Use F#? by staticassertion on Wednesday December 12, 2018 @11:36PM (#18669867)
If I want to learn F# is there a good IRC/slack channel I can get on? I'd like recommendations for libraries. I have a project I could throw at it for fun/ learning.

P.S.

Can someone explain this?

    // complex types in a few 
    type Employee = 
      | Worker of Person
      | Manager of Employee list
Re:Why Use F#? by phillipcarter on Thursday December 13, 2018 @02:56PM (#18675447)
Yes! If you join the F# Software Foundation[0] (100% free, no obligation to donate anything, etc.) you get access to a rather active slack.

[0]: http://foundation.fsharp.org/membership

Re:Why Use F#? by scriptdevil on Wednesday December 12, 2018 @11:46PM (#18669892)
Employee, Person and list are types Worker and Manager are constructors

Employee is a Sum Type i.e. it can either take a value of (Worker personObject) or (Manager [empObj1, empObj2...])

for instance, an binary tree would be

  type Tree a =
    | Leaf of a
    | Node of (Tree a * Tree a)
Now, a Tree Int would be

  t = Node (Node (Leaf 1, Leaf 2), Leaf 3)
Re:Why Use F#? by platz on Thursday December 13, 2018 @12:02AM (#18669953)

    abstract class Employee {}
    class Worker : Employee { 
      public Worker(Person o) {...}
    }
    class Manager : Employee { 
      public Manager(Employee[] o) {...}
    }
Re:Why Use F#? by sparkie on Thursday December 13, 2018 @04:25AM (#18671002)

    abstract sealed class Employee {

        enum TAG { Worker, Manager }
        
        readonly TAG tag;

        Employee(TAG t) {
            tag = t;
        }

        public class Worker : Employee {
            public Worker(Person p) : base(TAG.Worker) { ... }
        }

        public class Manager : Employee {
            public Manager(Employee[] o) : base(TAG.Manager) { ... }
        }
    }
This is a bit closer to how it is really implemented. The constructors are internal to the outer type because it is sealed, making it a closed type. Interestingly C# rejects abstract & sealed on the same type, although this is accepted on the CLR.

Discriminated unions are sometimes called tagged unions because of the way they're implemented. It's typically more efficient to check the tag on the object than it is to test its type. The tag is used when pattern matching over the object.

Re:Why Use F#? by platz on Thursday December 13, 2018 @10:01AM (#18672722)
Ok, the idea was to communicate to someone who has never seen an ADT before what kind of thing it is, rather than a full decompilation of how it's really implemented, but thanks for that too.