Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement dynamic: Compile-time interface check #5306

Closed
kekyo opened this issue Sep 17, 2015 · 5 comments
Closed

Improvement dynamic: Compile-time interface check #5306

kekyo opened this issue Sep 17, 2015 · 5 comments

Comments

@kekyo
Copy link

kekyo commented Sep 17, 2015

  • Sorry poor english...
  • Introducing the interface restrictions into dynamic type designated as a method argument is proposed.
  • For example:
//////////////////////////////////////////////
// C# 4.0 style dynamic usage:
public int Calculate(dynamic valueModel)
{
    // True runtime bind, compile time not checked.
    valueModel.DoubtfulValue = 123;
}

//////////////////////////////////////////////
// Propose partial compile-time checking syntax...
public interface IStrictValueModel
{
    int TrueDefinedValue { get; set; }
}

public int Calculate(dynamic IStrictValueModel valueModel)
{
    // Can check accessors on compile time.
    // (Beware, compile time check only, no change DSL infrastructure/IL based output.)
    valueModel.TrueDefinedValue = 123;
}

// Public signature means not changed.
// public int Calculate([Dynamic] object valueModel);
  • Pros:
    • Type-safe check on compile time.
    • IL code generator no change.
    • Metadata maybe change?
  • Cons:
    • Example is binding single interface type. If more improvement, use where clause?
    • No efficient IL code. Interface type metadata ignored.
@svick
Copy link
Contributor

svick commented Sep 17, 2015

This sounds like a duplicate of #3012 to me (only with different syntax).

@bondsbw
Copy link

bondsbw commented Sep 17, 2015

@svick Agreed, although I think I like this syntax better.

This one treats dynamic as a modifier, while the other (dynamic<IStrictValueModel>) looks like a generic type but is in no way similar to how generic types behave. For instance, in #3012 the resulting type has the relationship is-a with the type argument but generic types primarily use the type argument as method parameters and/or return values. The expectation is an implied definition dynamic<T> : T but that form of generic behavior doesn't work elsewhere in C#.

@kekyo
Copy link
Author

kekyo commented Sep 22, 2015

Thank you for your index to a similar article. The same discussion as argument in #3012 agreed.

I feel difficultly syntax extends...

  • Parenthesis: I feel looks like C# syntax, same typeof() operator, "dynamic(IABC, IDEF)".
  • Angle bracket: Looks like C++ template style, In C++ design, template expression is nearly cast clause (ex: static_cast, const_cast) or another usages (?). but in C# generic expression means only generic type declaration. there is no applicability.
  • Bracket not use: Combine of the types is incomprehensible. separator is white space?

I thought first:

Make the optional instance cast possible in the optional interface type. This is that impromptu is achieved:

void Hoge1()
{
  var model = new SealedModel(...);

  // example C++ like syntax:
  Hoge2(dynamic<IAnotherModel>(model));
}

void Hoge2(IAnotherModel model)
{
  // Implements with IAnotherModel type...
}

I think this code. It's necessary to add implement like impromptu to DLR. I find an unsolved problem to be included much... I thought the method on which I don't have other influence. That the problem which is most can't collateralize identity of an instance.

More improvement:

I think how to utilize type information for outside method implementation...

public int Calculate([Dynamic(typeof(IStrictValueModel))] object valueModel);

Add style information to the attribute. We may be able to use it by meta-programming and (vs) analyzers and caller-side type checking.

@bbarry
Copy link

bbarry commented Nov 9, 2015

What if this made use of generics and the type constraints that are already there:

public int Calculate<dynamic T>(T valueModel)
    where T : IStrictValueModel 
{
    ...

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants