#It looks like default values aren't

1 messages · Page 1 of 1 (latest)

chrome bobcat
#

I also expected that ⬆️ to work (cc @woven igloo) but I found I had to use a "constructor" pattern as well.
Same for class variables where I had to use an actual constructor vs default values.

This works:

import { dag, Container, Directory, object, func } from "@dagger.io/dagger"

@object()
class ChrisThomas {
  static readonly STAGING_URL: string;

  constructor() {
    this.STAGING_URL = "http://github.com/dagger/dagger";
  }

  @func()
  run(url?: string): Container {
    url = this.STAGING_URL;
    return dag.container().from("alpine:latest").withEnvVariable("STAGING_URL", url);
  }
}
woven igloo
#

It's not that your default value isn't handled, it's that you're passing it by reference to a variable.
Try to add a value directly and it should work:

@func()
  async run(url: string = "my_value"): Promise<Container> { }
leaden tartan
#

Ya.. that does work... and what I ended up using.
Just didn't expect the constant to not work

#

@woven igloo Do you actually need the default value, or are you just testing that its an optional field ?

woven igloo
#

It's because the introspector does not resolve the reference, this is super hard to do

#

I inject the default value to the graphQL schema yep

woven igloo
leaden tartan
#

I understand that resolving the value is almost impossible without running the code.
I would probably be nice to have a better error message though

chrome bobcat
#

Better error messages and docs to show the happy path are definitely on the roadmap. Thanks, @leaden tartan 🙏

leaden tartan
#

.. technically, you don't need to set the value in the constructor. You can just have it as a constant outside of the class itself.

#

There are so many "this doesn't work in 'dagger-flavoured typescript'" that it makes it a little annoying when you run into one of these

woven igloo
past stag
#

If you can't parse a default value, do you report it as optional, even if it's a non-null? That's what Python does. This allows Dagger to still make it optional and omit it, so that when the function is called in TypeScript, the default is used at runtime. If you're reporting it as optional and still not able to let TypeScript set the default at runtime, then there's something wrong with the way the function is being called.

leaden tartan
#

In Typescript, we can also use the result of a function as a default value 😛