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

Support for custom types? #131

Open
jheliker opened this issue Feb 6, 2023 · 11 comments
Open

Support for custom types? #131

jheliker opened this issue Feb 6, 2023 · 11 comments

Comments

@jheliker
Copy link

jheliker commented Feb 6, 2023

First of all - thanks for the fantastic library!

Question: how can we support custom variable types? For example in addition to DateTime my code often refers to TimeCode variables (for broadcast TV work). It is my understanding that these types with various properties cannot be utilized in Expressive?

If custom is not possible, perhaps we can get involved in contributing to support some additional types.

Thank you!

@bijington
Copy link
Owner

Thank you for the kind compliments.

You can use any types in Expressive but you will have limited support within the existing functions/operators.

As an example you could add in a TimeSpan as a variable and then have custom functions to work with them.

What are you hoping to achieve with this custom type?

@jheliker
Copy link
Author

Thanks for getting back to me. Time codes are used extensively in film, television, and digital media in place of regular time stamps. The primary purpose is to have alignment with individual video pictures called "frames" as the sub-second unit of measure in video files and streams.

I am planning to open-source a pure C# time code library that my company has been using for quite a while now in production. If you could help me understand how to integrate it into Expressive properly, I think it could benefit many people in the industry.

For reference, time code is standardized by the SMPTE organization and is often referred to as SMPTE time code: https://en.wikipedia.org/wiki/SMPTE_timecode. For anyone using Expressive in this space, they would probably find immense value in having built-in support for time codes.

@jheliker
Copy link
Author

@bijington Do you think you could describe what it would take to add native support for our Time Code library? we would be interested in taking on the work ourselves. We've gotten things to work partially through registering custom functions but it's pretty limited.

@bijington
Copy link
Owner

@jheliker I will have a think on this and try to come back with an approach as quickly as I can. I have always wanted to consider being able to add in new types such as NodaTime, etc. perhaps this will be the time to start considering this.

It would also be really nice to move away from the helper classes that do a lot of the logic around operator support.

Just to clarify what exactly do you need to support these Time Codes with? operators (+, -, *, /, etc.) and functions? Do you need to be able to do something like [timecode1] + [timecode2]?

If you would be able to clarify what you need that may help my understanding of how we can achieve this

@jheliker
Copy link
Author

Thanks, @bijington !

For our Time Code class, we have full operator overloads already implemented including IComparable and IEquatable and would love to be able to leverage all of it with Expressive if possible...

@bijington
Copy link
Owner

Thanks, @bijington !

For our Time Code class, we have full operator overloads already implemented including IComparable and IEquatable and would love to be able to leverage all of it with Expressive if possible...

Would you be willing to share your class? If not that's totally fine. I've just been thinking on whether I can expose certain interfaces if there aren't underlying ones to solve this

@jheliker
Copy link
Author

jheliker commented Mar 2, 2023

Hey @bijington - we've open-sourced our time code library as of this morning, please take a look!
https://github.com/middlemansoftware/TimeCode

@jheliker
Copy link
Author

Hey @bijington any feedback on the Time Code library? If you've got a direction we can work in, would be glad to take on the effort of getting native time code support in Expressive. Thank you!

@bijington
Copy link
Owner

Hey @jheliker sorry things have got in the way of getting around to coming up with a design for this. My initial thinking is to follow a similar concept to the generic maths additions to C#.

I like the idea of having interfaces like IAdditionOperator or something similar that will allow a custom type to provide operator support. Then we wouldn't have to have native support but it should make it really easy to register support for custom types.

With your TimeCode type, do you expect to be able to add other types to it e.g. int?

@bijington
Copy link
Owner

@jheliker what version of .NET are you building against? I would love to be able to achieve something like this: https://learn.microsoft.com/en-us/dotnet/api/system.numerics.iadditionoperators-3?view=net-7.0 but it would require jumping all the way up to .NET 7.0

@bijington
Copy link
Owner

This is far from working but it proves a general concept I think could work:

public interface IAdditionOperator
{
    object Add(object right);
}

public class TimeCode : IAdditionOperator
{
    public object Add(object right)
    {
        if (right is TimeCode2 timeCode2)
        {
            return this + timeCode2;
        }

        // TODO: What to do here?
    }

    public static TimeCode operator +(TimeCode tc1, TimeCode tc2)
    {
        if (tc1 is null)
        {
            throw new ArgumentNullException(nameof(tc1));
        }

        return tc1.Add(tc2);
    }

    public TimeCode Add(TimeCode timeCode)
    {
        // Actual magic from TimeCode class.
    }
}

public class Numbers
{
    public object Add(object lhs, object rhs)
    {
        if (lhs is IAdditionOperator additionOperator)
        {
            return additionOperator.Add(rhs);
        }

        // Remaining add code...
    }
}
  • IAdditionOperator would be a new set of interfaces that Expressive would expose.
  • TimeCode is a minimal copy from your repo with the interface above implemented.
  • Number shows how the current class in Expressive could be modified.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants