Skip to content

[SUGGESTION] const variables with deduced types #190

Closed

Description

@leejy12

Cppfront compiles

cx : const = 42;

into

auto cx { 42 };

which omits the const. I think we should support const variables with deduced types such that the original Cpp2 code produce something like

auto const cx { 42 };

Activity

  1. jarzec commented on Jan 3, 2023

    @jarzec
    Contributor

    I can try this one - this looks like a perfect issue for a beginner. I already had a look at it in code version from a couple of days back.
    @hsutter I saw you cleaned up the treatment of qualifiers. Those update should make fixing this much easier.

  2. hsutter commented on Jan 4, 2023

    @hsutter
    Owner

    Good point, thanks for the bug report. I intended that you have to write the type too if you say anything about the type. Eventually I might allow a wildcard there (e.g., cx : const _ = 42;, that isn't supported today).

    I'm now adding a diagnostic... thanks!

    Update: @jarzec sorry for missing your note!

  3. gregmarr commented on Jan 4, 2023

    @gregmarr
    Contributor

    Good point, thanks for the bug report. I intended that you have to write the type too if you say anything about the type. Eventually I might allow a wildcard there (e.g., cx : const _ = 42;, that isn't supported today).

    My understanding is that cx := 42 is equivalent to cx : auto = 42 and thus auto cx = 42; so it would make sense that cx : const = 42 would be equivalent to cx : const auto = 42 and thus const auto cx = 42;

    Since you've said that there is not going to be a volatile qualifier, but rather a type, are there any other type modifiers that could appear in a variable declaration? If there are others, then it may make sense to require a "wildcard type", but if that's the only one, then it's likely clear enough.

  4. hsutter commented on Jan 4, 2023

    @hsutter
    Owner

    OK, this has pushed me to finish adding _ wildcards for local variables and is expressions, as they already work in parameter types and inspect... done!

    This test case is now checked in... line 1's and 2's parameters value and x always worked, but the other uses of _ didn't work before, and now newly do work:

    less_than: (value:_) -> _   // now works, but see note below
        = :(x:_) = x < value$;
    
    main: () -> int
    = {
        x: const   _ = 2;       // now works
        p: *       _ = x&;      // now works
        q: * const _ = p&;      // now works
    
        if x is (less_than(20)) {
            std::cout << "yes, less\n";
        }
    
        if x is _ {             // now works
            std::cout << "yes, always\n";
        }
    }
    

    Note that the less_than example has a deduced return type, which as discussed elsewhere is not order-independent. My aim is to allow non-local deduction in just those cases where it doesn't violate order-independence, such as when deducing from literals in the other thread where this was discussed, but the implementation is currently in a 'partway' state that's successively approximating toward that goal and doesn't currently reject all the code that won't be order-independent. It will need a future diagnostic to reject those cases, or we could decide that it's okay to teach "a function with a deduced return type has to be defined before it's called" as a specific exception to strict order independence. The deduced return type is admittedly useful for lambda generator functions.

  5. gregmarr commented on Jan 4, 2023

    @gregmarr
    Contributor

    Looks good.

    When you have some time, if you could explain a little why the _ is important there, it would be appreciated. I'm not seeing the importance, so having some other perspectives would be useful. No rush. (If you've already done that somewhere else, and I've just missed it or forgotten, a reference would be fine.)

  6. hsutter commented on Jan 4, 2023

    @hsutter
    Owner

    You mean for the variable declaration cases...

    My feeling was that it's fine to just write nothing between : and = to deduce the type, but it felt odd to allow saying const and nothing more... maybe I just need to get used to it! I'll keep it in mind.

  7. gregmarr commented on Jan 4, 2023

    @gregmarr
    Contributor

    I guess that question would also apply to these, unless they're also optional and you're just showing how they can be provided if wanted: (value:_) -> _ = :(x:_). I think it makes sense that it's needed in x is _ {.

    Maybe unrelated question, in x is (less_than(20)), are the outer () required to indicate an expression and not a type, or can this be x is less_than(20)?

  8. hsutter commented on Jan 4, 2023

    @hsutter
    Owner

    Yes, those parens are currently required around is (predicate).

  9. gregmarr commented on Jan 4, 2023

    @gregmarr
    Contributor

    Thanks.

  10. jarzec commented on Jan 4, 2023

    @jarzec
    Contributor

    @hsutter When I saw your first answer in this thread I though that as much as I like the idea of having _ as a wildcard I think it might be problematic for Cpp1 compatibility reasons. It is unlikely to be seen in actual code but the following is valid Cpp1:

    struct _ {};
    

    Is this not an issue? Of course the problem lies not in the idea of a wildcard but in the choice of the character - it should not be something usable in identifiers. In fact I have been using the fact that _ is a valid identifier in Cpp1 to use it as a "wildcard" for unused names in structured bindings. A proper wildcard would be so much nicer also there.
    Maybe ? would do? The choice is yours of course, I could try to make the change that should be on even simpler change 😁.

  11. hsutter commented on Jan 5, 2023

    @hsutter
    Owner

    @jarzec Nope, that shouldn't be an issue because Cpp2 sits in front of the existing language and is in control even of words that are popular in existing code (like potentially _) or reserved in Cpp1 (like new). Cppfront just translates them to something else (e.g., cpp2_new) when lowering to Cpp1, and that lets Cpp2 do things like treat new as an ordinary overloadable member function template because the translated name really is a function.

    I just tried this example and it works fine:

    //  Here's some existing Cpp1 code that provides the name "_"
    //  (not advisable at global scope like this, but legal and fine)
    struct _ {
        struct Nested { };
    };
    
    //  Cpp2 can still use it fine, with qualification
    main: ()->int = {
        y: _::Nested = ();
    }
    

    In cppfront I've tried to translate _ to auto only in the cases where Cpp2 treats it as a wildcard. If I missed any or was too aggressive just post a bug report and I'll take a look. But normally the idea is that you can still use a Cpp1 name that conflicts with a Cpp2 special word legal in the same position (such as a wildcard) could exist just by qualifying the Cpp1 name.

    (I just pushed a commit that makes using ::_ work in the above example. It only didn't work because there was a bug where I wasn't recognizing any ::name as a qualified-id, that's now fixed.)

  12. jarzec commented on Jan 5, 2023

    @jarzec
    Contributor

    I was more thinking of the particular use in inspect as in this modified bit of mixed-inspect-templates.cpp2:

    struct _ {};
    
    // Can I take the _ struct as argument?
    fun: (v : _) -> std::string = {
        return inspect v -> std::string {
            is std::vector  = "std::vector";
            is std::array   = "std::array";
            is std::variant = "std::variant";
            is my_type      = "my_type";
            // Can I handle the _ struct?
            is _ = "unknown";
        };
    }
    
  13. gregmarr commented on Jan 5, 2023

    @gregmarr
    Contributor

    I suspect the answer is "no, you can't", and I'm okay with that answer. I'm okay with losing the ability to name a type or variable as just _ in CPP2 in order to gain the ability to have a "wildcard type" or "discarded variable" capability as in C#, Typescript, Go (and the internet says also Python, Dart, and Haskell).

    To be clear, this is just about "in CPP2 code", which already is different than existing code. It should still have the same meaning as before in CPP1.

  14. hsutter commented on Jan 5, 2023

    @hsutter
    Owner

    @jarzec:

       // Can I handle the _ struct?
       is _ = "unknown";
    

    Again, great question. Yes, just qualify the name... this program works:

    struct _ {};
    
    fun: (v : _) -> std::string = {
        return inspect v -> std::string {
            is ::_          = "::_ matched";
            is _            = "unknown";
        };
    }
    
    main: ()->int = {
        x: ::_ = ();
        std::cout << fun(x);    // prints "::_ matched"
    }
    

    @gregmarr:

    I suspect the answer is "no, you can't", and I'm okay with that answer.

    I would be okay with it too, but I like that the answer is "yes, you can"... Being able to demonstrate compatibility with even these kinds of crazy-ish names is an important part of being able to 100% seamlessly consume existing C++ code, which is the primary "special sauce" goal that sets this project apart from all others, past and present.

  15. gregmarr commented on Jan 6, 2023

    @gregmarr
    Contributor

    Oooh, even better. I hadn't considered consuming a CPP1 type in a CPP2 function, so it's good that it works.

  16. added 2 commits that reference this issue on Feb 24, 2023
    9a56a50
    333ac9b
  17. added a commit that references this issue on Feb 4, 2024
    9770422
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions