Giter VIP home page Giter VIP logo

dotvariant's People

Contributors

jvbsl avatar mknejp avatar theblubb14 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

dotvariant's Issues

Excpetion throw in Dispose method

Hello,
in Dispose method always throw exception Result has encountered an internal error. Please file....

Example code:

    [Variant]
    partial class Result
    {
        static partial void VariantOf(object result, Exception exception, IDisposable handle);
    }

Usage:

using Result result = Result.Create(new Diposable());

Better space-efficiency with Unsafe.*

[Variant]
readonly partial struct SceneObjectParameter
{
	static partial void VariantOf(
		double @double,
		float @float,
		int @int
	);
}

internally uses

private readonly struct Union
{
	double @double;
	float @float;
	int @int;
}

but a more space-efficient structure can be used instead:

[StructLayout(LayoutKind.Explicit, Size = 8)]
private readonly struct Union
{
	double @double;
}

with the use of Unsafe.BitCast<TFrom,TTo>/Unsafe.As<TFrom,TTo> for access.

Reference types can use a single object.

Determine visibility of generated extension methods based on variant types

Currently all generated extension methods are public regardless of the visibility of the variant type.

Thus the following generates invalid code because the generated extensions have inconsistent accessibility:

namespace Foo
{
    internal class A { }

    [dotVariant.Variant]
    public partial class Variant
    {
        static partial void VariantOf(A a);
    }
}

// generated code:
public static class VariantEx
{
  // CS0051 and CS0057: Inconsistent accessibility
  public static void Match(this Variant v, out A a) { ... }
}

Handle types implementing `IAsyncDisposable`

Right now disposing is only handled for IDisposable. However if a type implements IAsyncDisposable the variant should implement that as well and delegate asynchronous disposing to the live value where appropriate.

Skip emptiness handling if type cannot be empty

If a variant class has no default constructor and all other constructors initialize the _variant field to a non-default value then the variant cannot be empty. Under these circumstances the "empty" case is impossible to achieve and can be removed from the interface as these code paths will never execute.

Possible conflict: if constructors are later changed and the empty state becomes possible the previously safe methods could now throw InvalidOperationException. Maybe keeping the empty state is the safer option as it reminds the user of this eventuality. This needs investigating.

Did you have a look at F#

Hello Miro Knejp, I dont know if you have had a look at another .Net Language that actually has discriminated union types or know that there is a language that already supports it. It is I guess not as efficient as your version but it is supported at language level. Utilizing the interoperability of F# and C# there are ways to use them from C# but it is quite cumbersome. This is just FYI and not really an Issue.

Can not compile Nested Variant with Generics from Parent

I have following Pattern

[Variant]
public class Parent<TInput, TOutput, TValue, TExternalValue>
{
    static partial void VariantOf(
            Notification<string> notification,
            Parent<TInput, TOutput, TValue, TExternalValue>.ChildA a,
            Parent<TInput, TOutput, TValue, TExternalValue>.ChildB b
        );

    [Variant]
    public partial class ChildA
    {
        static partial void VariantOf(
            string name,
            TInput input,
            TExternalValue value
        );
    }

    [Variant]
    public partial class ChildB
    {
        static partial void VariantOf(
            TOutput output,
            TValue value,
            Action action
        );
    }
}

I get now a build issue that ChildAEx can not Build because it don't know about TExternalValue

Allow user to define special methods

If the user already defines certain special methods (like ToString or Equals) don't generate them.
This is currently only supported for Dispose.

Candidates:

  • Constructors
  • ToString()
  • Equals() and GetHashCode()

Users then need to refer to the private _variant field to get the necessary information to implement these.

Support nested variants

Right now variant classes are only supported at namespace-level. Attempting to create one as a nested class will generate invalid code.

Finer grained determination of visibility for extension methods

Currently the generated static class containing extension methods is either public or internal based on the visibility of the variant itself, but it doesn't take into consideration the visibility of the value types.

In addition to that, now that we support generic types we can end up in a scenario like the following:

public partial class Variant<T1> { /* ... */ }

internal partial class Variant<T2, T2> { /* ... */ }

// generated code:
internal static class VariantEx
{
  // Extension methods for Variant<T1> are public
  ...
  // Extension methods for Variant<T1, T2> are public
  ...
}

Even though this is a very questionable design, users can write code like that and it should not generate a broken implementation.

To support this properly we need to determine the visibility for the extension class itself separately from the visibility of the generated extension methods. So in the above example VariantEx should be public with the extension methods for Variant<T1> generated public and for Variant<T1, T2> generated internal.

Generics support

Hi,

Is it currently possible to create a variant with a generic parameter? e.g.

[Variant]
public partial struct Result<T>
{
    static partial void VariantOf(T success, string[] errors);
}

Currently I'm getting the following error when I try:

Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName contains an invalid character '<' at position 22. (Parameter 'hintName')'

Thanks.

Determine visibility of generated interface based on variant types

Currently all generated methods are public regardless of the visibility of the contained type.

Thus the following generates invalid code because the generated members have inconsistent accessibility:

internal class A { }

[Variant]
public class Variant
{
  static partial void VariantOf(A a);

  // generated code:
  // CS0051 and CS0057: Inconsistent accessibility
  public void Match(out A a) { ... }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.