Skip to content

Latest commit

 

History

History
73 lines (50 loc) · 2.04 KB

answers.md

File metadata and controls

73 lines (50 loc) · 2.04 KB

Chapter 3 Questions and Answers

1. This query is trying to display every NPC along with the name plus every City type for each NPC, but it's giving an error. What is it missing?

All it needs is parentheses around the select - remember, putting it in parentheses "captures" the output so it can be used.

select NPC {
  name,
  cities := (select City.name)
};

2. If the City type needed a required property called population, what would it look like? What type would 'population' be?

Right now City is just extending Place:

type City extending Place;

So it would need a required property for population:

type City extending Place {
  required population: int32;
};

int16 has a maximum value of 32,767 while both int32 and int64 are large enough, but int32's maximum value of 2,147,483,647 is certainly enough for a city.

3. This query wants to display name twice for some reason but is giving an error. Can you think of a way to do it?

You can access the name property twice by giving it a different name the second time. Let's call it name2:

select Person {
  name,
  name2 := .name
};

4. People keep trying to make characters with negative ages. Can you think of a constraint that can stop this?

We haven't seen this constraint before but we have seen max_value() so it's easy to guess: with min_value() you can do it. With these two constraints, HumanAge must be between 0 and 120:

scalar type HumanAge extending int16 {
  constraint max_value(120);
  constraint min_value(0);
}

5. Can you insert a HumanAge type?

No, because it's a scalar type and not an object - a HumanAge would just be an int16 connected to nothing.

However, you can select a HumanAge by casting:

select <HumanAge>16;

This gives {16}.

You can also see that a HumanAge is just a different name for an int16 by trying the following:

select <HumanAge>16 is int16;
select <HumanAge>16 is HumanAge;

Both of these return {true}.