Skip to content

Feature: Allow Opt for optional params along with Option - #99

Open
moigagoo wants to merge 7 commits into
masterfrom
feature/opt
Open

Feature: Allow Opt for optional params along with Option#99
moigagoo wants to merge 7 commits into
masterfrom
feature/opt

Conversation

@moigagoo

Copy link
Copy Markdown

Closes #87

Comment thread presto/route.nim
let bodyName = bodyArgument.name
res.add(quote do:
let `bodyName` {.used.}: Opt[ContentBody] =
if `bodyParam`.isSome():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this game here needed? is bodyParam an Option?

@moigagoo moigagoo Aug 6, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That's needed because bodyParam is an Option in RestApiCallback and RestΑpiCallback2: https://github.com/status-im/nim-presto/blob/master/presto/route.nim#L22-L33

I could make it a generic type (i.e. RestApiCallback2[T: Option[ContentBody] or Opt[ContentBody] = ... body: T) and all the types and procs that use it as well... I'll see if that's viable.

Comment thread presto/route.nim Outdated
when defined(metrics):
import metrics

converter toOpt*[T](x: Option[T]): Opt[T] =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Converters have global effects well beyond Presto. Not sure this is a bad conversion to have in place as such, but definitely tradeoffs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The tradeoff is between modifying a significant portion of the code in Presto or introducing potentially undesired conversions. Making RestApiCallback2 compatible with Opt is totally doable without a converter, it's just that a lot more code will have to change.

TBH the initial solution with converting Option to Opt in processApiCall doesn't look that bad in comparison.

I'm currently trying to bake something without a converter and explicit Option → Opt conversion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this converter won't fly for many reasons, chief among them:

  • Opt and Option semantically differ in how they handle ref
  • they introduce the expensive copy that we're looking to avoid by using a single type throughout

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ie even RestApiCallback3 is the better option here - the code should be generic over Opt/Option so that the whole pipeline is done without extraneous conversions

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.

Add support for Opt as an optional type

3 participants