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.
some syntactical ideas for C#
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 };
// 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!");
}
};
}
// without 'where' there'd some some ambiguity when parsing collection initializers
List<int> DisambigRequired()
{
// also, methods should default to "public" inside wheres
// 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;
}
// tuple syntax
void ALaPython()
{
var (x, y, z) = (1, 2, 3);
var (x1, y1) = (1, 2);
var (x2,) = (1,);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment