Skip to content

Instantly share code, notes, and snippets.

@Porges
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save Porges/9654760 to your computer and use it in GitHub Desktop.

Select an option

Save Porges/9654760 to your computer and use it in GitHub Desktop.

Revisions

  1. Porges revised this gist Mar 21, 2014. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions syntax.cs
    Original file line number Diff line number Diff line change
    @@ -93,4 +93,17 @@ void ALaPython()
    {
    return (1, "hello");
    }



    /* and a few more items; */
    void Misc()
    {
    // default-assign operator
    int? x = null;
    x ??= 2;

    // binary literals
    var n = 0b1010;
    }
    }
  2. Porges revised this gist Mar 21, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions syntax.cs
    Original file line number Diff line number Diff line change
    @@ -82,9 +82,9 @@ void EveryoneKnowsThisAlready()
    [Implemented(true)]
    void ALaPython()
    {
    var (x, y, z) = (1, 2, 3);
    var (x1, y1) = (1, 2);
    var (x2,) = (1,);
    var x = (1, 2, 3);
    var y = (1, 2);
    var z = (1,);
    }

    // and in return types:
  3. Porges revised this gist Mar 21, 2014. 1 changed file with 8 additions and 15 deletions.
    23 changes: 8 additions & 15 deletions syntax.cs
    Original file line number Diff line number Diff line change
    @@ -52,21 +52,6 @@ IDisposable Whatever()
    return new List<int> where IDisposable { void Dispose() { Console.WriteLine("Disposed"); } }
    }

    // allow ?? overload
    public struct MyMaybe<T>
    {
    T GetValueOrDefault(T other)
    {
    throw new NotImplementedException();
    }
    }

    void OverloadQQ()
    {
    var x = new MyMaybe<int>();
    int k = x ?? 1; // statically resolves to GetValueOrDefault
    }

    // combine these features:
    void FairlyTerribleCode<T>()
    {
    @@ -94,10 +79,18 @@ void EveryoneKnowsThisAlready()
    }

    // tuple syntax
    [Implemented(true)]
    void ALaPython()
    {
    var (x, y, z) = (1, 2, 3);
    var (x1, y1) = (1, 2);
    var (x2,) = (1,);
    }

    // and in return types:
    [Implemented(true)]
    (int, string) Foo()
    {
    return (1, "hello");
    }
    }
  4. Porges revised this gist Mar 21, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion syntax.cs
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@ IDisposable Whatever()
    }

    // allow ?? overload
    struct MyMaybe<T>
    public struct MyMaybe<T>
    {
    T GetValueOrDefault(T other)
    {
  5. Porges revised this gist Mar 21, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions syntax.cs
    Original file line number Diff line number Diff line change
    @@ -92,4 +92,12 @@ void EveryoneKnowsThisAlready()
    var y = tmp.Item2;
    var z = tmp.Item3;
    }

    // tuple syntax
    void ALaPython()
    {
    var (x, y, z) = (1, 2, 3);
    var (x1, y1) = (1, 2);
    var (x2,) = (1,);
    }
    }
  6. Porges revised this gist Mar 21, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion syntax.cs
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,6 @@ public override string ToString()
    Console.WriteLine("HAHA!");
    }
    };
    return x;
    }

    // without 'where' there'd some some ambiguity when parsing collection initializers
  7. Porges renamed this gist Mar 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. Porges revised this gist Mar 21, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,14 @@
    class Example
    {
    // "init-anywhere" syntax (along the lines of Haskell)
    [Implemented(true)]
    Foo Do()
    {
    var x = new Foo { bar = 1 }; // <- currently available
    return x { bar = 3 }; // <- proposed (note that this mutates unlike Haskell)
    }

    [Implemented(true)]
    Foo Also()
    {
    var x = new Foo { bar = 1 };
  9. Porges revised this gist Mar 21, 2014. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -42,4 +42,53 @@ List<int> DisambigRequired()
    // not private!
    return new List<int> where { int Length { return Count; } }
    }

    // also implement new interfaces
    IDisposable Whatever()
    {
    // how about:

    return new List<int> where IDisposable { void Dispose() { Console.WriteLine("Disposed"); } }
    }

    // allow ?? overload
    struct MyMaybe<T>
    {
    T GetValueOrDefault(T other)
    {
    throw new NotImplementedException();
    }
    }

    void OverloadQQ()
    {
    var x = new MyMaybe<int>();
    int k = x ?? 1; // statically resolves to GetValueOrDefault
    }

    // combine these features:
    void FairlyTerribleCode<T>()
    {
    var magicList = new List<T> where {
    List<T> GetValueOrDefault(T elseWise)
    {
    if (Count == 0) Add(elseWise);
    return this;
    }
    }

    var stupidity = (magicList ?? default(T)).First();
    }

    // tuple unpacking
    void EveryoneKnowsThisAlready()
    {
    var (x, y, z) = Tuple.Create(1, 2, 3);

    // expands to:
    var tmp = Tuple.Create(1, 2, 3);
    var x = tmp.Item1;
    var y = tmp.Item2;
    var z = tmp.Item3;
    }
    }
  10. Porges revised this gist Mar 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ public override string ToString()
    return x;
    }

    // without 'where' there'd some some ambiguity when parsing initializers
    // without 'where' there'd some some ambiguity when parsing collection initializers
    List<int> DisambigRequired()
    {
    // also, methods should default to "public" inside wheres
  11. Porges revised this gist Mar 20, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ public override string ToString()
    return x;
    }

    // without 'where' there'd some some ambiguity when parsing initializers
    List<int> DisambigRequired()
    {
    // also, methods should default to "public" inside wheres
  12. Porges renamed this gist Mar 20, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. Porges created this gist Mar 20, 2014.
    44 changes: 44 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    class Example
    {
    // "init-anywhere" syntax (along the lines of Haskell)
    Foo Do()
    {
    var x = new Foo { bar = 1 }; // <- currently available
    return x { bar = 3 }; // <- proposed (note that this mutates unlike Haskell)
    }

    Foo Also()
    {
    var x = new Foo { bar = 1 };

    // this could be used akin to VB 'with' ;)
    x {
    bar = 2,
    baz = 3,
    };

    return x;
    }

    // And for Java-parity:
    // - reuse keyword 'where' to do anonymous inner classes (it's needed to disambig)
    public void YEAH()
    {
    var x = new Foo { bar = 1 }
    where
    {
    public override string ToString()
    {
    Console.WriteLine("HAHA!");
    }
    };
    return x;
    }

    List<int> DisambigRequired()
    {
    // also, methods should default to "public" inside wheres
    // not private!
    return new List<int> where { int Length { return Count; } }
    }
    }