I'm working with dart and let's say I have an enumeration (potentially really long) that I cannot modify:
enum Animal {
cat,
dog,
lion,
tigger,
}
Is it possible to restrict this enum, I would like to obtain
enum DomesticAnimal {
cat,
dog,
}
with DomesticAnimal.cat still being an Animal ?
Also, is it possible to extend it to obtain
enum LivingCreature {
cat,
dog,
lion,
tigger,
tree,
flower,
}
where LivingCreature.cat still being an Animal ?
No. There is currently nothing like that in Dart. An enum value object belongs to exactly one enum type.
There is also no way to create new type which contains just some instances of other types.
Related
As the title states, I need to tell grails GORM that a domain class has a many to many relationship to itself.
In my system, I have a class "course", this "course" class can have none, one or many correlatives.
Taking the example:
mathematical analysis 2
To be able to take the mathematical analysis 2 course, you need to have approved mathematical analysis 1 and physics courses.
mathematical analysis 1 does not have any correlatives or courses to be approved to be able to anyone to take the course.
I have the following:
class Course {
String name
static hasMany = [correlatives: Course]
static belongsTo = [target_course: Course]
}
But it does not seem to have the impact in the Data base that I expect.
Im expecting to have a course_course table that has the many to many to itself like the following:
course_course
int id_course_course
int id_correlative_course
int id_target_course
Am I missing something important here?, help please!
You don't need a many to many table to represent that. While each Course may "have many" correlatives, each correlated course only has one parent.
This is sufficiently represented by a single table like (heavily depending on any mappings you specify):
course:
id (int)
name (varchar)
target_course_id (int, nullable)
I have a structure identical to this post here:
How do I create a recursive one-to-many relationship using core data?
It looks something like this:
cage ---------- animal 1
|
|_____ animal 2
|
|_____ animal 3 ____ animal 4
|
|__ animal 5
|
|_____ animal 6
And I have implemented my models exactly as the correct answer has done, i.e.
The problem for me, is that with this structure, only animal 1 has a non-nil property cage, but I would like ALL descendent animals to have this property cage so that i could query the cage property animal6.cage.
I've tried setting this manually but the inverse relationship causes any animal with a cage property to be a direct child of that cage, which I don't want.
Is it possible to inherit the cage property for each animal?
You're using terms like "inherit" and "child" in ways that don't have meaning to Core Data. You have Cage which is related to Animal. Animal has a relationship to itself.
There's no parent/child relationship or inheritance here as far as Core Data is concerned. If one Animal is related to another, they're just two instances with a relationship. One can't inherit a value from the other because one is not the "parent" in any sense that Core Data uses. The two instances are two independent objects and they don't inherit anything any more than any two non-Core Data objects would.
Following from that, setting the cage property doesn't make an Animal a "direct child" of the cage, it just says it's related to the cage. If you want to find the cage for any arbitrary Animal without setting cage on every instance, you need to do something like (Swift-ish pseudocode):
func cage(for animal:Animal) -> Cage {
var currentAnimal = animal
var cage = currentAnimal.cage
while cage == nil && currentAnimal.parent != nil {
currentAnimal = currentAnimal.parent
cage = currentAnimal.cage
}
return cage
}
That's fine if you just want to find the cage for an animal, but you can't use it in a fetch request. If you need something you can use when fetching, you probably need to add a second relationship from Animal to Cage so that you can distinguish the "parent" animal from any others. Every Animal would have a value for one of the relationships, and the other relationship would be reserved for the parent.
My use case is based on the following model:
struct Person {
let name: String
let houses: [House]
}
struct House {
let owner: Person
}
Now, ideally I would like to maintain a bidirectional relationship that requires every house to have exactly one owner where an owner should also know all its houses.
Using the above data structures, is it possible to create instances of House and Person such that there is a relationship between the two and the objects are essentially pointing at each other?
I guess the phrasing of this already is somewhat misleading, because due to the value semantics of structs, they don't really point anywhere but are only holding copies of values. It seems to be like it should be obvious that it's not possible to create these two objects with a bidirectional relationship, but I still wanted to be sure and ask this questions here!
An obvious solution would also be to make houses and owner variables using var instead of let when declaring them, then the relationship could be maintained in the initializer of each struct:
struct Person {
let name: String
var houses: [House]
init(name: String, houses: [House]) {
self.name = name
self.houses = houses
self.houses = houses.map { (house: House) in
var newHouse = house
newHouse.owner = self
return newHouse
}
}
}
struct House {
var owner: Person
init(owner: Person) {
self.owner = owner
var newHouses = self.owner.houses
newHouses.append(self)
self.owner = Person(name: owner.name, houses: newHouses)
}
}
However, what if I want to keep houses and owner constant? As I said, it seems to be obvious that it's not possible, but I'm wondering if there's some fancy (maybe functional) way to achieve this? I was thinking about lenses, which can be used as getters and setters when dealing with immutable models.
What you're describing sounds more like an ORM than a language feature, and also doesn't sound appropriate to handle with value types like structs. How do you expect the language to know that owner is the inverse relationship property of houses and needs to be maintained accordingly? This is something that needs to be enforced with code is not possible with the Swift language alone.
It sounds like you're trying to maintain some kind of model object graph, which is a problem that's been solved many times over. You should take a look at Realm DB and Core Data both of which offer managed bidirectional relationships. You'll find however that neither of these are implemented with structs. The problem with using value types is that they are copy-on-write. As soon as you mutate one struct in the object graph, you'd need to reassign all of it's related things to the new, mutated copy, which then in turn are mutated and would need to update all of their related structs. Reference semantics (classes) make maintaining an object graph much easier as mutations don't produce new instances.
What you want is not possible using structs. Your current solution (with var) doesn't work like you think it does: all values are independent copies. Consider the following example:
let p = Person(name: "Nick", houses: [])
let h = House(owner: p)
h.owner.houses
p.houses
Because each copy is independent, this means p isn't the same as h.owner.
If you need a bidirectional relationship, you should use objects with weak pointers (to break the reference cycle). Alternatively, you could think to see if you need the bidirectional relationship.
I have to sort the stores registered in segments. I want to have pre-registered segments for the user to choose, but they are many. It's a good idea to put everyone in an enum? There are over 15.
A Store is a model that have a type, the type is the segment. There is several types. The pre-registered are the several types in enum. I was thinking that the code will be dirty with all types in enum.
It's up to you, really. You'll have people tell you that it's better to manage something like a field that can only have one of several values as an enum, and others would say that the field should be a string and you should validate it on your Segments model.
I'm partial to the latter.
If I had a Dog model and you can choose it to be 1 out of 3 of the Types called Small, Medium and Large. Should these Types be models themselves if I'm going to put logic in them? What would be the model design?
If Small, Medium and Large will each have unique methods but share common attributes you could have a base Dog model and then subclass each of the sizes like class SmallDog < Dog. Use single table inheritance on the dogs table by adding a type column that accepts values like "SmallDog", etc.
Will all the different types have a common interface but just differ in their logic?
If the interface is the same (i.e they all have the same function definitions) then I'd just have subclasses for each of the different types of dog that extend the original Dog model and have some kind of factory class that handles the creation of Dog models and automatically selects the appropriate class based on the type of Dog. By using the factory class with a common interface for Dog types the rest of the application does not need to care about the type of Dog and you can freely add/remove new types by simply modifying the factory class.