Skip to content

QueryOne!: Support /./ to immediately get term + -default when 0 results#265

Merged
osnr merged 3 commits into
mainfrom
osnr/queryone-dot-and-default
May 30, 2026
Merged

QueryOne!: Support /./ to immediately get term + -default when 0 results#265
osnr merged 3 commits into
mainfrom
osnr/queryone-dot-and-default

Conversation

@osnr
Copy link
Copy Markdown
Collaborator

@osnr osnr commented May 29, 2026

Saves a lot of code repetition -- we've been using lindex, dict getdef, and list length comparisons very repetitively when querying imperatively.

image image

See Discord: https://discord.com/channels/956758212152025098/956765077883744266/1510000054507802785

image

Need to test that this didn't break anything.

osnr added 2 commits May 29, 2026 16:22
@osnr osnr requested review from cwervo, ppkn and smj-edison May 29, 2026 20:55
}
set formats [QueryOne! the paper formats are /./]
set formatName [QueryOne! paper format /./ is the default paper format \
-default letter]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you allow flags to be passed in after query parameters? I suppose this is more of an edge case, but -- might be needed in the future.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, I think we allow flags anywhere? + it reads better this way, I think, without the letter right up next to paper

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like the idea of eventually supporting --

}
try {
dict set options printer \
[QueryOne! printer /./ is the default printer]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you want a flag like -takefirst instead of try/on here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I suppose this is just in case nothing is returned, never mind.

@s-ol
Copy link
Copy Markdown
Collaborator

s-ol commented May 29, 2026

Neat. /./ is a new bit of "syntax", right? I'm wondering if using uplevel to match When's binding logic could make sense:

AssignOne! the paper formats are /formats/
# equivalent to
set formats [QueryOne! the paper formats are /./]

# enables multiple capture (still enforcing single overall match)
AssignOne! the /medium/ formats are /formats/

You could also specify defaults inline, although that requires introducing specialized syntax once again:

AssignOne! the /medium=paper/ formats are /formats={letter a4}/

@osnr
Copy link
Copy Markdown
Collaborator Author

osnr commented May 30, 2026

Neat. /./ is a new bit of "syntax", right? I'm wondering if using uplevel to match When's binding logic could make sense:

AssignOne! the paper formats are /formats/
# equivalent to
set formats [QueryOne! the paper formats are /./]

# enables multiple capture (still enforcing single overall match)
AssignOne! the /medium/ formats are /formats/

You could also specify defaults inline, although that requires introducing specialized syntax once again:

AssignOne! the /medium=paper/ formats are /formats={letter a4}/

I like this idea. Reads more cleanly than set x .... We were talking about Expect or Observe for a reactive variant of this. This could be Expect!, maybe. @smj-edison what do you think?

@smj-edison
Copy link
Copy Markdown
Collaborator

Agreed, that's much cleaner. Just a note, but if someone does /name=$default/ it'll substitute into a single string without any escaping, so I guess parse accordingly. Also, the example with /formats={letter a4}/ will parse as two tokens, since the parser is in normal string mode since it didn't open with a brace. It would have to be {/formats=letter a4/}.

@osnr
Copy link
Copy Markdown
Collaborator Author

osnr commented May 30, 2026

image

@osnr
Copy link
Copy Markdown
Collaborator Author

osnr commented May 30, 2026

Maybe we introduce Expect! with no defaults for now, since that covers a lot of the cases here, then think about further syntax later?

Expect! the fps is /fps or 3/ could work as well if we're going to parse multiple tokens anyway

@smj-edison
Copy link
Copy Markdown
Collaborator

So essentially scan ahead until you see a token ending in /? That makes a lot of sense. String injection is also harder to pull off because of Tcl's escaping rules.

@smj-edison
Copy link
Copy Markdown
Collaborator

Oops, I forgot to respond to Expect!. I think it's a good idea to introduce it as Expect! now, and we can extend it later.

@smj-edison
Copy link
Copy Markdown
Collaborator

Oh further thought, and at risk of breaking Tcl's string transparency, when you evaluate a function like Expect!, you could look at the script being evaluated, and you could make sure that the trailing / comes from a string token and not from a cobbled together source.

for the suggestion. We can use it almost anywhere that we don't have a
default, and it reads a lot cleaner.
@osnr osnr merged commit c0733be into main May 30, 2026
@osnr osnr deleted the osnr/queryone-dot-and-default branch May 30, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants