Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Setting values in a child object from a parent object #1058

Closed Answered by HT154
jmgilman asked this question in Q&A
Discussion options

class A {
 name: String
 b: Mapping<String, B>
}
class B {
 name: String
}

In this simplified example, is it possible to have B.name default to the value of A.name if it's not specified? I understand the existence of the Mapping is what complicates things here, but it's at the crux of the question. It's possible I'm using the wrong type here.

You must be logged in to vote

If I'm interpreting your goal correctly, I think you want something like this:

class A {
 name: String
 local _a = this // this "captures" this A so it can be used unambiguously in inner scopes
 b: Mapping<String, B> = new {
 default {
 name = _a.name
 }
 }
}
class B {
 name: String
}

So writing this:

test = new A {
 name = "test"
 b {
 ["abc"] {}
 ["def"] { name = "def" }
 }
}

Produces

test {
 name = "test"
 b {
 ["abc"] {
 name = "test"
 }
 ["def"] {
 name = "def"
 }
 }
}

Replies: 2 comments 1 reply

Comment options

If I'm interpreting your goal correctly, I think you want something like this:

class A {
 name: String
 local _a = this // this "captures" this A so it can be used unambiguously in inner scopes
 b: Mapping<String, B> = new {
 default {
 name = _a.name
 }
 }
}
class B {
 name: String
}

So writing this:

test = new A {
 name = "test"
 b {
 ["abc"] {}
 ["def"] { name = "def" }
 }
}

Produces

test {
 name = "test"
 b {
 ["abc"] {
 name = "test"
 }
 ["def"] {
 name = "def"
 }
 }
}
You must be logged in to vote
0 replies
Answer selected by jmgilman
Comment options

Ah, it was the default syntax that I didn't know of. Thanks!

You must be logged in to vote
1 reply
Comment options

HT154 May 4, 2025
Maintainer

No problem! It's worth noting that default isn't actually special syntax, it's just a regular property of the Mapping instead of a k/v entry. I wrote a little about how this all works (and link some relevant documentation) here: #1053 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /