Skip to content

Svelte 5: Introduce $derived.with rune #9968

Closed
@Not-Jayden

Description

@Not-Jayden

Describe the problem

This is a bit of a rehashing of #9250 prompted by seeing more of this discussion coming up in the Discord

TL:DR there's still animosity around people's preferences regarding the design choice for $derived accepting an expression instead of a callback function, and how it becomes slightly less ideal for the scenarios where you want to encapsulate more complex logic that wraps onto multiple lines.

Describe the proposed solution

Introduce a $derived.with rune, which accepts a callback for determining the derived value instead of an expression.

e.g.

const total = $derived.with(() => {
  const discountAmount = cartTotal * promoDiscount;
  const taxedAmount = (cartTotal - discountAmount) * taxRate;

  return cartTotal - discountAmount + taxedAmount;
});

Alternatives considered

  • Keep using IIFE's or defining the logic in functions outside of $derived, given it's really not that big of a deal
  • A separate $computed rune instead of not being nested under $derived
  • Alternative method names: call, compute, using, from

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions