#dang constructors

1 messages · Page 1 of 1 (latest)

graceful stream
#

for this, yeah honestly I'll probably need to add support for explicit constructors.

the closest thing you can do is this:

⁨```graphql
type MyType {
let privateArg: String!
}


because ⁨`privateArg`⁩ is ⁨`String!`⁩, Dang will make it a required constructor arg (otherwise you'd have a ⁨`null`⁩ non-null!), and because it's ⁨`let`⁩ it won't be a public field

but, if you do ⁨`let: privateArg: String! = "default"`⁩ it won't be an arg because it has a value present
last imp
#

In this particular case:

type Rust {
  """Source directory."""
  pub source: Directory! @defaultPath(path:"/")

  """Version (image tag) to use from the official image repository as a base container."""
  pub version: String! = "latest"

  """Container to use as a base container."""
  pub base: Container! = container().from("rust:" + version)
}

I want to be able to set a container, OR set a version and use the default container.

I'm able to do that with other SDKs and constructors.

Frankly, I'm not sure what's the best solution.

It might be a good temporary one to introduce the same @private annotation as in the Go SDK. That doesn't require a language change, like constructors would.

graceful stream
#

We'll need constructors for our immediate plans anyway, since they involve a special type that can't be saved as a field (Workspace). And currently constructor args are derived from fields

graceful stream
#

dang constructors

graceful stream
#

@last imp for https://github.com/vito/dang/issues/24 - what if I just made constructors behave like functions with a hardcoded expected return value? so your example would work, and in other cases you just add an explicit self return (which should be already somewhat familiar from other chaining methods)

GitHub

Given the following example: import Dagger """Rust programming language module.""" type Rust { """Source directory""" pub source: Directo...

#

going with that for now, feels most natural

last imp
#

Works for me

graceful stream
#

@last imp fixed all 3 issues, thanks again for the reports! 🙏

graceful stream
#

@last imp just fyi, i pushed a change to switch dang fmt from tabs to two spaces, pinging in case you were about to commit a big reformat, to avoid another one 😛

last imp
last imp
graceful stream
#

the LSP integration supports it, so for me it just formats on save now. the Zed extension has been published too

#

to do a one-off, dang fmt -wl **/*.dang

#

no toolchain at the moment - probably a good idea now that generators have shipped

last imp
#

I wonder: with the latest changes, would something like this work?

  new(
    """Source directory"""
    source: Directory! @defaultPath(path:"/")

    """Version (image tag) to use from the official image repository as a base container"""
    version: String! = "latest"

    """Container to use as a base container"""
    container: Container! = Dagger.container().from("rust:" + version)
  ) {
#

it would make an elegant way to set default values, but it's not that much better than

    self.container = container ?? Dagger.container().from("rust:" + (version ?? "latest"))
#

(BTW I think I'll just use Dagger. and not import Dagger unless I have to. I like the explicit nature of it)

graceful stream
last imp
graceful stream
#

update: it wasn't difficult

graceful stream