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

Create a definitely if and definitely while macro #236

Open
masak opened this issue Jul 9, 2017 · 3 comments
Open

Create a definitely if and definitely while macro #236

masak opened this issue Jul 9, 2017 · 3 comments

Comments

@masak
Copy link
Owner

masak commented Jul 9, 2017

The recent-ish Perl 6 addition of the keywords with, orwith, without is (IMHO) a rare missed opportunity to abstract away a common solution to a set of related problems, and providing that common solution as a single thing, for example a single keyword.

Perhaps things could've looked differently if macros had been further progressed in Perl 6-land. I dunno.

Here I propose such an abstracted solution. 007 can try it out and see how it fits. The proposal adds the statement form definitely, with code that looks like this:

definitely if foo() -> value {
    # ...
}

Which desugars to the following (⦃⦄ indicating hygiene):

if ⦃ my temp = foo(); temp != None && temp ⦄ -> value {
    # ...
}

The form else definitely if should also work under this proposal. (Corresponding to Perl 6's orwith.)

There is no unless in 007 (corresponding to Perl 6's without), but if there ever were a serious one, I'd expect definitely unless to work out-of-the box by seamlessly composing with this macro. (But I might settle for a good explanation why that wouldn't work so easily, or be useless, or whatever. It sure seems that the block parameter of a definitely unless would be a lot less useful.) If it does work, I'd expect it to work through a conditional statement protocol somehow.

I'd expect definitely while to work along very similar principles. Ditto ditto definitely until, see above.

I wouldn't expect the definitely form to work with an (also macro-provided) ?? !! (see #163). Mostly since definitely would be a statement form, and ?? !! an operator.

@masak masak changed the title Create an definitely if and definitely while macro Create a definitely if and definitely while macro Jul 9, 2017
@b2gills
Copy link

b2gills commented Jul 9, 2017

I think allowing where as part of the conditional would be more generally useful.
(I do realise that it seems awkward)

if foo() where .defined -> \value { # block would get run if `foo` returned `0`
    # ...
}
if file-foo() where :e -> \filename {
    # ...
}

I do think it is worthwhile to proceed with implementing definitely if as you stated.
As this is the sort of feature I would like to be able to easily create in the future.

@0racle
Copy link

0racle commented Jul 10, 2017

@b2gills Both of your examples are already provided by grep.

if &{ False }().grep(*.defined) -> $x { say ~$x }     # OUTPUT: 'False'

Some crazy people already floated the idea of aliasing grep as where but that idea got shot down 😄

@masak's suggestions seems more to do with replacing 3 new keywords with 1 qualifying keyword that can slot in front of already existing branching constructs. If that is a correct assessment, then I like the idea.

@masak
Copy link
Owner Author

masak commented Jul 10, 2017

@masak's suggestions seems more to do with replacing 3 new keywords with 1 qualifying keyword that can slot in front of already existing branching constructs. If that is a correct assessment, then I like the idea.

That's right.

But see also #237, which is a solution in expression-land to a partially overlapping problem. 😄

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

No branches or pull requests

3 participants