Git Product home page Git Product logo

Comments (7)

JakeWharton avatar JakeWharton commented on June 10, 2024

I'd also be interested in the ability to specify a nullable default. For example, in the case of this file we default to a path specified by a system property which may not be present.

val path by parser.storing("The path", ::File)
    .optional()
    .default(System.env("THE_PATH")?.let(::File))

from kotlin-argparser.

xenomachina avatar xenomachina commented on June 10, 2024

Does something like this do what you want?

val path by parser.storing<File?>("The path", ::File)
        .default(System.getenv("THE_PATH")?.let(::File))

It would be nice if a nullable default made the Delegate's type nullable. Actually, it would be nice if in in general setting the default could "relax" the type of the delegate (that is, make the Delegate into a Delegate where S is a super-type of T and the default value).

I'm currently investigating to see if there's a way to do this in Kotlin's type system.

from kotlin-argparser.

JakeWharton avatar JakeWharton commented on June 10, 2024

Hmm I was under the impression that the internals required a non-null value but I might have been mistaken. I will give your suggestion a try today.

from kotlin-argparser.

xenomachina avatar xenomachina commented on June 10, 2024

If they do, that is probably a bug. I jumped through some hoops to distinguish between "not set" and "set to null".

from kotlin-argparser.

JakeWharton avatar JakeWharton commented on June 10, 2024

Ok so it works for File? when explicitly specified and which allows the use of default(null) (or some nullable thing).

It's a bit awkward for nullable string arguments though as you have to pass an identity function because the non-transformer overload doesn't have a generic parameter.

val title by parser.storing<String?>("Execution title", { this }).default(null)

from kotlin-argparser.

xenomachina avatar xenomachina commented on June 10, 2024

That's a good point. Commit 1bc1474 changes the way default works that fixes this. It makes default into an extension method, and so now the default value can be nullable, actually null, or even some other type. The resulting property will have the most specific superclass. In the case of starting with a Delegate<String>, you'll get a Delegate<String?> if the default is null or String?, regardless of which factory method created the original.

It's a somewhat backwards incompatible change, as Delegate is now an abstract class instead of an interface. Really, I probably should have done that from the beginning, as I never meant for people to be able to create their own Delegate implementations.

from kotlin-argparser.

xenomachina avatar xenomachina commented on June 10, 2024

I'm going to close this as I think 1bc1474 should solve the root issue.

from kotlin-argparser.

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.