Giter Site home page Giter Site logo

Any plans on making get public? about sumtype HOT 8 CLOSED

pbackus avatar pbackus commented on August 11, 2024
Any plans on making get public?

from sumtype.

Comments (8)

pbackus avatar pbackus commented on August 11, 2024

No, get will never be public.

If you want something similar, I suggest a tryGet function like the following:

T tryGet(T, ST)(ST st)
    if (isSumType!ST)
{
    return st.tryMatch!((T value) => value);
}

This will throw an exception when the requested type is not found.

from sumtype.

iK4tsu avatar iK4tsu commented on August 11, 2024

Yes, however that doesn't work with betterC. If not get, then maybe a @system version of it. But if it isn't in your plans, which is understandable, then I'll just keep using match.

That being said, I don't think a function to extract the value directly would damage the lib in any way, if it were @system and well documented. It would come down to the user's choice if he wanted to use the function or not.

from sumtype.

pbackus avatar pbackus commented on August 11, 2024

Good API design is as much about what you leave out as about what you include. I want sumtype to guide users into the pit of success, and providing a function that crashes at runtime if you use it incorrectly is contrary to that goal.

from sumtype.

iK4tsu avatar iK4tsu commented on August 11, 2024

How about adding a version of tryMatch/tryGet suitable for betterC. This would assert if the requested type wasn't found.

from sumtype.

pbackus avatar pbackus commented on August 11, 2024

You can write it yourself if you really want it. I will not include such a function in the sumtype library, for the reason stated above.

from sumtype.

FFY00 avatar FFY00 commented on August 11, 2024

Hold my beer while I fork the standard library for my project... because I want to use something in betterC...?

from sumtype.

iK4tsu avatar iK4tsu commented on August 11, 2024

I don't know if this covers all cases, but based on your example above:

T tryGet(T, ST)(ST st)
	if (isSumType!ST)
{
  	static if (st.Types.length == 1)
      	return st.match!((T value) => value);
  	else
      	return st.match!(
        	(T value) => value,
        	function T (_) { assert(false); }
    	);
}

Not really what I want, but it might do as a workaround.

from sumtype.

pbackus avatar pbackus commented on August 11, 2024

Personally I would recommend something like this for BetterC (and in general, honestly):

Nullable!T get(T, ST)(ST st)
    if (isSumType!ST)
{
    return st.match!(
        (value) {
            static if (is(typeof(value) == T))
                return nullable(value);
            else
                return Nullable!T();
        }
    );
}

from sumtype.

Related Issues (20)

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.