Skip to content

Commit

Permalink
Replace is operator with switch pattern matching (#98)
Browse files Browse the repository at this point in the history
* Replace is operator with switch pattern matching and as operator

* Switch back to is operator
  • Loading branch information
gpetrou authored and slide committed Feb 5, 2018
1 parent a2e21a1 commit 8e4c8eb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,16 @@ private void SplatDictionaryArgument(IList<string> splattedNames, IList<DynamicM
IDictionaryEnumerator dictEnum = dict.GetEnumerator();
while (dictEnum.MoveNext()) {
DictionaryEntry de = dictEnum.Entry;
if (de.Key is string) {
splattedNames.Add((string)de.Key);

if (de.Key is string s) {
splattedNames.Add(s);
splattedArgs.Add(
DynamicMetaObject.Create(
de.Value,
Ast.Call(
AstUtils.Convert(dictMo.Expression, typeof(IDictionary)),
typeof(IDictionary).GetMethod("get_Item"),
AstUtils.Constant(de.Key as string)
AstUtils.Constant(s)
)
)
);
Expand Down
4 changes: 2 additions & 2 deletions Src/Microsoft.Dynamic/ComInterop/ComBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ private static bool TryGetMetaObjectInvoke(ref DynamicMetaObject instance) {
return true;
}

if (instance.Value is IPseudoComObject) {
instance = ((IPseudoComObject)instance.Value).GetMetaObject(instance.Expression);
if (instance.Value is IPseudoComObject o) {
instance = o.GetMetaObject(instance.Expression);
return true;
}

Expand Down
41 changes: 24 additions & 17 deletions Src/Microsoft.Dynamic/Runtime/CodeDomCodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Runtime {

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")] // TODO: fix
public abstract class CodeDomCodeGen {
// This is the key used in the UserData of the CodeDom objects to track
Expand Down Expand Up @@ -55,43 +56,49 @@ private SourceUnit CreateSourceUnit(LanguageContext context, string path, Source
return src;
}

virtual protected void WriteArgumentReferenceExpression(CodeArgumentReferenceExpression e) {
protected virtual void WriteArgumentReferenceExpression(CodeArgumentReferenceExpression e) {
_writer.Write(e.ParameterName);
}

virtual protected void WriteSnippetExpression(CodeSnippetExpression e) {
protected virtual void WriteSnippetExpression(CodeSnippetExpression e) {
_writer.Write(e.Value);
}

virtual protected void WriteSnippetStatement(CodeSnippetStatement s) {
protected virtual void WriteSnippetStatement(CodeSnippetStatement s) {
_writer.Write(s.Value);
_writer.Write('\n');
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] // TODO: fix
protected void WriteStatement(CodeStatement s) {
// Save statement source location
if (s.LinePragma != null) {
_writer.MapLocation(s.LinePragma);
}

if (s is CodeExpressionStatement) {
WriteExpressionStatement((CodeExpressionStatement)s);
} else if (s is CodeSnippetStatement) {
WriteSnippetStatement((CodeSnippetStatement)s);
switch (s) {
case CodeExpressionStatement statement:
WriteExpressionStatement(statement);
break;
case CodeSnippetStatement statement:
WriteSnippetStatement(statement);
break;
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] // TODO: fix
protected void WriteExpression(CodeExpression e) {
if (e is CodeSnippetExpression) {
WriteSnippetExpression((CodeSnippetExpression)e);
} else if (e is CodePrimitiveExpression) {
WritePrimitiveExpression((CodePrimitiveExpression)e);
} else if (e is CodeMethodInvokeExpression) {
WriteCallExpression((CodeMethodInvokeExpression)e);
} else if (e is CodeArgumentReferenceExpression) {
WriteArgumentReferenceExpression((CodeArgumentReferenceExpression)e);
switch (e) {
case CodeSnippetExpression expression:
WriteSnippetExpression(expression);
break;
case CodePrimitiveExpression expression:
WritePrimitiveExpression(expression);
break;
case CodeMethodInvokeExpression expression:
WriteCallExpression(expression);
break;
case CodeArgumentReferenceExpression expression:
WriteArgumentReferenceExpression(expression);
break;
}
}

Expand Down
18 changes: 10 additions & 8 deletions Src/Microsoft.Dynamic/Utils/WeakDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,16 @@ public T GetObjectFromId(int id) {
public int GetIdFromObject(T value) {
lock (_synchObject) {
foreach (KeyValuePair<int, object> kv in _dict) {
if (kv.Value is WeakObject) {
object target = ((WeakObject)kv.Value).Target;
if (target != null && target.Equals(value))
return kv.Key;
} else if (kv.Value is T) {
object target = (T)(kv.Value);
if (target.Equals(value))
return kv.Key;
switch (kv.Value) {
case WeakObject weakObject:
object target = weakObject.Target;
if (target != null && target.Equals(value))
return kv.Key;
break;
case T t:
if (t.Equals(value))
return kv.Key;
break;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Src/Microsoft.Scripting/Runtime/StringDictionaryExpando.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public override DynamicMetaObject BindDeleteMember(DeleteMemberBinder binder) {

public override IEnumerable<string> GetDynamicMemberNames() {
foreach (object o in _keys) {
if (o is string) {
yield return (string)o;
if (o is string s) {
yield return s;
}
}
}
Expand Down

0 comments on commit 8e4c8eb

Please sign in to comment.