question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(Proposal) Concepts/Traits (enhanced generic constraints)

See original GitHub issue

Traits

Traits are a specification a Type / Method must have to be valid. This specification is validation at both compile-time and runtime. The runtime cost maybe worth it for additional safety,

Basic Grammar

        trait ::= "Trait" identifier specification
specification ::= '{' spec+ '}'
         spec ::= 

incomplete

A trait can be define in a separate construct, like a class / struct.

Examples

trait Zero { static readonly Zero ()->T }
trait Unit { static readonly Unit ()->T }
trait [+] ( static [+] (T,T)->T } // operator + (addition)
trait [-] { static [-] (T,T)->T } // operator - (subtraction)

A trait can “inherit” other existing traits. eg.

trait SimpleCalc <: { Zero, Unit, [+], [-] }

This allows “constrained generics” over numeric types.

Summation< T > ( IEnumerable<T> nums )
  T has SimpleCalc
{
  T total = T.Zero();
  foreach( T num in nums)
    total = total + num; // += can't be used as the specification didn't specify it.
  return total;
}

A trait can also be anonymously define at the point of use

foo <T> ()
  T has Trait { ... }
{
 // method code 
}

Value Traits

Value Trait are checked against the value of a variable, if it possible to valid at compile-time it is, if not it is validated at runtime.

 trait NonNull <T : class> { != null }


int CharCount ( string s )
  s is NonNull 
{
  return s.Length;
}

Note: T! could be an alias for the NonNull Trait


I do require help to spec-out this idea, I think it has potential has *(as see it) that traits would also enable other current proposals / part of them.

  • NonNull #98
  • Method Contracts #119
  • Constraints aspect of #104

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:46 (28 by maintainers)

github_iconTop GitHub Comments

1reaction
mirhagkcommented, Jan 29, 2015

I like this very much. It allows the generics in C# to be much more flexible, allowing structural typing for methods and classes that use the traits.

This is a really good basis to develop other features on top of in the future. For instance it’d be nice if you could have automatic traits, where the compiler infers the traits based on the usage within the class/function. That would eliminate a LOT of the boiler plate of types in C#, and give you essentially global type inference in opt-in scenarios.

1reaction
fubar-codercommented, Jan 28, 2015

I’d like to use a trait in a similar way I’d use an interface:

public trait Zero<T> {
    static T Zero { get; }
}
public trait Add<T> {
    static T operator +(T,T);
}
public trait Subtract<T> { 
    static T operator -(T,T);
}

Inheritance:

public trait SimpleCalc<T> : Zero<T>, Add<T>, Subtract<T> {
}

Usage:

T Summation<T>(IEnumerable<T> nums)
    where T has SimpleCalc<T>
{
  var total = T.Zero;
  foreach( T num in nums)
    total = total + num; // += can't be used as the specification didn't specify it.
  return total;
}

I personally won’t bother about value traits and leave this kind of checks/validations to the Method contracts.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Parameters Proposal
This is the design for adding generic programming using type parameters to the Go ... Type parameters are constrained by interface types.
Read more >
Generic typealiases with additional constraints? - Compiler
This makes sense: generic typealiases with additional constraints are not "simple type synonyms" for some existing type. If you want to add ...
Read more >
Constraints on type parameters - C# Programming Guide
The constraint enables the generic class to use the Employee.Name property. The constraint specifies that all items of type T are guaranteed to ......
Read more >
Why Do Humans Reason? Arguments for an Argumentative Theory
Abstract: Reasoning is generally seen as a means to improve knowledge and make better decisions. However, much evidence shows.
Read more >
photojournalistic messages on online newspapers in vietnam ...
From there, propose solutions and recommendations to improve the quality of photojournalism messages in online newspapers in Vietnam today.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found