If I have class A which has a dependency on class B, then class B could be passed in to the ctor of class A.
What about if class B has a dependency on class C, does that mean class A should receive all required dependencies upon construction ?
In general terms, Dependency Injection would suggest that your classes should have passed all dependencies in the constructor.
However, for your example, it seems to me that A depends on B and B depends on C. In other words, A only needs to have passed B in the constructor; because B will already be constructed using the C instance. In other words, if we wrote the code without a DI framework:
C c = new C();
B b = new B(c);
A a = new A(b);
Related
func yields an error - A value of type "Object" can't be returned from the function 'func' because it has a return type of 'B'. Why this error specifically? Shouldn't both C() and D() be considered to fit B and therefore have the ternary expression evaluate to an instance of B? If not, why does the ternary expression evaluate to an Object and not an instance of A? This error does not occur using regular conditional syntax (if/else).
class A {}
class B {}
class C extends A implements B {}
class D extends A implements B {}
B func(bool b) => b ? C() : D();
Turns out this is a known issue - see the issues linked here: https://github.com/dart-lang/language/issues/2577
I have an EMF metamodel containing Abstract Class A and Concrete Classes B & C implement A.
In XText I can have a rule such as A: B | C; and refer to A in other rules.
Is there a way not to have to declare the rule for A and have a default rule used for abstract classes (which would be OR(All concrete children)) by Just providing the rules for B and C?
it is not possible to call rules you don't have nor a catch all children
if you just want to have child rules and add a parent to them without a explicit rule for the parent
you can do
B returns A:
{B} "b" name=ID;
I have the following purescript code:
class Node a where
parentNode :: forall b. (Node b) => a -> b
but when compiling this I get the following error:
A cycle appears in the definition of type synonym Node
Cycles are disallowed because they can lead to loops in the type checker.
Consider using a 'newtype' instead.
I am trying to write a function parentNode that returns the parent node of a node. The only guarantee for the parent node is that it is also a Node b.
I do not care what the actual type for b is.
I am basically trying to say, parentNode should be a function that returns a value that also implements the Node typeclass. Is something like this possible with type classes or is there some other idiomatic way to do this type of thing?
The type of your parentNode function says that the caller can choose the type b of the parent, but I think this is incorrect. The type should be determined by what's in the DOM, so you need an existential type.
The technical issue here is that type classes cannot currently refer to themselves.
However, in this case, I think there is a simpler solution which doesn't use classes. Why not something like this?
class IsNode a where
toNode :: a -> Node
foreign import data Node :: *
foreign import parentNode :: Node -> Node
I'm attempting to use Railway Oriented Programming principals http://fsharpforfunandprofit.com/rop/ and this http://indiedevspot.azurewebsites.net/2015/01/20/two-track-coding-rop-for-dummies-part-2/ for reference
I have successfully implemented this for most of the codebase, except now we are getting to putting items into SQL and wish to use ROP for validation of those types. The typical pattern is
Figure 1:
let createSomething a b c = {A = a; B = b; C = c}
let createValidSomething so =
createSomething
<!> validateFunction1 so.param1
<*> validateFunction2 so.param2
<*> ...so forth and so on
You will notice that createSomething is a function that returns a record type instantiation a -> b -> c -> a' -> b' -> c'
The SQL Type providers return a mutable type (non record). Lets look at my attempt to build a similar createSomething function
Figure 2:
let createSQS(a, b, c, d, e, f, g) =
let sqs = context.``[dbo].[Something]``.Create(a, b, c, d, e, f, g)
sqs
At this point, we know this already will not work, we have a->b->c->d->e->f->g->context.[dbo].[Something].Entity
I know I can have an intermediary record type and follow ROP principals, match on success/failure and then create my object off of the already validated. But does that not seem like too many steps?
Does anybody know of a good pattern for this? Ideally we could have a function similar to Figure1 that generates a Record Type that is compatible with the Type Providers.
I'm open to trying things and hanging out on Skype :).
Should your function createSQS not be better like this:
let createSQS a b c d e f g =
context.``[dbo].[Something]``.Create(a, b, c, d, e, f, g)
This one would have the needed signature of a->b->c->d->e->f->g->context.[dbo].[Something].Entity
So we came up with an answer at our dev meeting, and decided that we in fact WANT to execute each part in its own function. For the inner binds we invented our own operator; similar to plus (&&&) (which I will blog about on my site www.indiedevspot.com) and at the top level we ran with a normal bind.
Our top level code looks like this:
[<HttpPost>]
x.member Add(sa:Something) =
sa|>ValidateSomething
>>= converttoSSA
>>= persistSSA
We decided to separate the concerns due to independent testability of validation, conversion and persistence. The theory is that functional composition if made up of functions that are guaranteed to work, is itself inherently guaranteed to work (or have a much better chance).
If we went with the proposed method (which we have not yet solved), we would be mixing concerns of creation, validation and conversion. That would also end up creating an additional record type which was un-necessary.
Until now, I had a class like this one:
type C<'a when 'a :> A> (...)
But now I created a new type B:
type B (...) =
inherit A()
But I don't want C to support B, and this doesn't compile:
type C<'a when 'a :> A and not 'a :> B> (...)
How can I do that?
You can't and shouldn't. If B is an A, then C should handle it. If it's reasonable for C not to be able to handle B, then B shouldn't derive from A. Otherwise you're effectively breaking Liskov's Substitution Principle (or at least a variant of the same).
When you declare that B inherits from A, you're saying that it can be used as an A. If that's not the case, you shouldn't be using inheritance.