-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 dot-like method calls #19384
Conversation
We need to enable |
What if it expanded to Or even: |
Yeah, I agree. So far (unless I'm missing something) an infix node always has exactly two children. That's nice to deal with in macros. Turning infix into something that might have N children would be a bit annoying and break more macro logic than necessary. |
Ok this is more inline with reality. |
I've been told that this isn't strictly true, but it certainly is unexpected. Let's go through the alternatives that have been mentioned:
|
Oh, wow. I had no idea. Seems to me like that example should ideally also be some different AST, but I suppose it's sensible.
Personally, I'd vote for 3 (with a preference excluding
after all (or I'd expect to be able to). I dislike 1, because it's not really a "prefix". If anything I dislike 2, because it's not really a dot expression either. It may look like it, but that makes it seem like |
Updated to produce a |
AFAICT this has been enabled in #19598 |
Correct. |
Can this be merged? |
Looks like a hack to me. |
@@ -480,6 +483,16 @@ proc dotLikeExpr(p: var Parser, a: PNode): PNode = | |||
result.add(opNode) | |||
result.add(a) | |||
result.add(parseSymbol(p, smAfterDot)) | |||
if p.tok.tokType == tkParLe and p.tok.strongSpaceA <= 0: | |||
var call = newNodeI(nkCall, info) | |||
let operator = opNode.ident.s & "()" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks rather hacky. What is it that we're trying to achieve here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's parsing a.?b(c)
as `.?()`(a, b, c)
, so that it can be matched by a template or macro.
So what is |
Yes, this PR parses Without this PR, What would you recommend as a good AST for |
But I don't know what you need to accomplish but |
Thanks, I see your point. I would love to have something closer to
I could rewrite the code in this PR to return the first form, which has the added benefit that it's backward compatible with any code that was written before dot-like syntax was introduced. But it also doesn't feel very natural to me.
I wrote a library called questionable that is heavily inspired by how Swift handles optionals. One example of its use is: import std/sequtils[
import pkg/questionable
let a: ?seq[int] = @[41, 42].some
let b: ?seq[int] = seq[int].none
let x = a.?distribute(2).?len() # equals 2.some
let y = b.?distribute(2).?len() # equals int.none As you can see, the I could switch over the library to e.g. the |
Maybe we should undo -d:nimPreviewDotLikeOps instead if it doesn't deliver what it promised. |
That would solve my problem, yes. |
I hope the It works well for my usage. template `.?`*[T: ref](x: Reactive[T], y: untyped{ident}): untyped =
cast[T](x.value).y
type
Card = ref object
id: int
Counter = ref object
num: int
card: Card
y := Counter(card: Card(id: 16))
watch:
console.log "card: ", y.?card.id
y.?card.id += 1 Without
|
Please don't add flags that change the way programs compile, it's a nightmare for every library developer.. |
I agree. We can parse it like before. |
the idea with the preview flags is that library developers can enable them to see if the feature being enabled is both useful and doesn't break other useful stuff - when a feature has "proven it's worth" it can be enabled more broadly. It's important that potentially breaking features gain wider reach and distribution than sitting unnoticed in there should probably be a |
A new feature was added behind a preview flag, we enabled it on devel in order to detect problems, we detected problems, now we can undo or refine the feature. The process works. |
Should have been more precise: I don't have anything against preview flags, as mentioned they are useful. So please, fix it or revert it, don't keep it as a alternate flag forever |
#19919 has already clarified that - |
You can still handle these expressions without |
Add support for calls using a custom operator that starts with a dot, such as
a.?b(c)
This implements a small part of RFC341, proposal 2.