Get children of Dom_html.element - dom

In js_of_ocaml, is it possible to get the child nodes of Dom_html.element?
I know that the class inherits Dom.node, and thus has the childNodes method. But since it is a method from Dom.node, it returns values of Dom.node types. And I need those nodes to still be Dom_html.element, or else most methods will not be available.
Since downcasting is not possible in OCaml, I do not find any possible solution for this issue. Am I missing something or is this really impossible?

childNodes cant't be typed as a collection of Dom_html.elements because the nodes returned can, and are likely to, include nodes that are not elements, such as text nodes.
The DOM standard defines a property children on Element which would only return the elements, but that still wouldn't get you to Dom_html.element. And unfortunately it also does not seem to be included in JSOO's Dom.element.
You can use the element function of Dom.CoerceTo to safely coerce Dom.nodes to Dom.elements, but I don't think there is any generally reliable way to go from Dom.element to Dom_html.element, because the DOM is unfortunately too dynamically typed.
You might have to check the tagName manually and (unsafely) cast it using Js.Unsafe.coerce.

Related

Is there a simple way to filter & narrow collections on instance type in assertj?

Can this be written as a single line?
assertThat(actualDeltas)
.filteredOn(delta -> delta instanceof Replacement)
.asInstanceOf(InstanceOfAssertFactories.list(Replacement.class))
I expected asInstanceOf to do the filtering. Alternatively, I searched for extractors or other concepts, but couldn't find any simple solution.
Is that possible with assertj?
By design, the purpose of asInstanceOf is only to provide type-narrowed assertions for cases where the type of the object under assertion is not visible at compile time.
When you provide InstanceOfAssertFactories.list(Replacement.class) as a parameter for asInstanceOf, you are telling AssertJ that you expect the object under assertion to be a List with elements of type Replacement.
While asInstanceOf will make sure that the object under test is a List, it will neither filter nor enforce that all the list elements are of type Replacement. The Replacement will ensure type-safety with subsequent methods that can be chained, for example with extracting(Function).
Currently, filteredOn(Predicate) or any other filteredOn variant is the right way to take out elements that should not be part of the assertion. If the filtering would happen outside (e.g., via Stream API), no asInstanceOf call would be needed as assertThat() could detect the proper element type based on the input declaration.

Does it make sense to have properties like Option[List[...]] when the List container supports the property of empty?

Does it make sense for class properties or function parameters that are like:
Option[List[String]]
Option[Map[String,String]]
Does wrapping it in an option provide more composability or since the container e.g. List and Map supports being empty there is no point in making it an Option?
Is it more efficient to have Option since it can be a None versus having a List.empty or Map.empty?
It depends ;)
Option is also a collection - it just can't have more than 1 element (but it can have 0). So it usually doesn't make sense to wrap another collection in Option.
However, it depends on what it represents and what semantics it conveys. Is None and empty List the same? Or maybe it matters that the List is there, but it's empty? And it's different than when the List is not there?
Depends on a use case. An empty list could actually mean something different than absence of a list.
Does Option[Int] make sense? Maybe not, if you just want to represent a number of candies (0 will do). But what if you are looking for a maximum element of a list, which happens to be empty? None would be much more appropriate in this case than 0.
Same with lists and other things. If you are listing brands of candy in someone's pocket, sure, you can use Nil to cover the case of a poor shmuck who doesn't have any.
But what if we wanted to describe, say a store inventory?
def listBrandsInInventory(product: String = "candy"): List[String]
What if this is a bookstore, and doesn't carry candy at all? Sure, you could argue, that nobody needs a store that does not carry candy, or you could still just return Nil (and it wouldn't even be lying), but there are two different situations here: either all candy is sold out, or we don't carry candy at all (don't bother to call tomorrow). If you'd like to distinguish between the two, Option[List] comes handy.

How can I list all customElements defined?

In looking at CustomElementRegistry type I cannot find any method that would allow me to iterate over all customElements defined.
Is there a way to enumerate over existing customElements without knowing their 'names' (as per .define() 'name' parameter)?
The only way that I know of would be to override customElements.define and have your function save off the info you want and then return the value from the original call. Then things still work the same, except that you can track everything in your own data.

scala - is it possible to force immutability on an object?

I mean if there's some declarative way to prevent an object from changing any of it's members.
In the following example
class student(var name:String)
val s = new student("John")
"s" has been declared as a val, so it will always point to the same student.
But is there some way to prevent s.name from being changed by just declaring it like immutable???
Or the only solution is to declare everything as val, and manually force immutability?
No, it's not possible to declare something immutable. You have to enforce immutability yourself, by not allowing anyone to change it, that is remove all ways of modifying the class.
Someone can still modify it using reflection, but that's another story.
Scala doesn't enforce that, so there is no way to know. There is, however, an interesting compiler-plugin project named pusca (I guess it stands for Pure-Scala). Pure is defined there as not mutating a non-local variable and being side-effect free (e.g. not printing to the console)—so that calling a pure method repeatedly will always yield the same result (what is called referentially transparent).
I haven't tried out that plug-in myself, so I can't say if it's any stable or usable already.
There is no way that Scala could do this generally.
Consider the following hypothetical example:
class Student(var name : String, var course : Course)
def stuff(course : Course) {
magically_pure_val s = new Student("Fredzilla", course)
someFunctionOfStudent(s)
genericHigherOrderFunction(s, someFunctionOfStudent)
course.someMethod()
}
The pitfalls for any attempt to actually implement that magically_pure_val keyword are:
someFunctionOfStudent takes an arbitrary student, and isn't implemented in this compilation unit. It was written/compiled knowing that Student consists of two mutable fields. How do we know it doesn't actually mutate them?
genericHigherOrderFunction is even worse; it's going to take our Student and a function of Student, but it's written polymorphically. Whether or not it actually mutates s depends on what its other arguments are; determining that at compile time with full generality requires solving the Halting Problem.
Let's assume we could get around that (maybe we could set some secret flags that mean exceptions get raised if the s object is actually mutated, though personally I wouldn't find that good enough). What about that course field? Does course.someMethod() mutate it? That method call isn't invoked from s directly.
Worse than that, we only know that we'll have passed in an instance of Course or some subclass of Course. So even if we are able to analyze a particular implementation of Course and Course.someMethod and conclude that this is safe, someone can always add a new subclass of Course whose implementation of someMethod mutates the Course.
There's simply no way for the compiler to check that a given object cannot be mutated. The pusca plugin mentioned by 0__ appears to detect purity the same way Mercury does; by ensuring that every method is known from its signature to be either pure or impure, and by raising a compiler error if the implementation of anything declared to be pure does anything that could cause impurity (unless the programmer promises that the method is pure anyway).[1]
This is quite a different from simply declaring a value to be completely (and deeply) immutable and expecting the compiler to notice if any of the code that could touch it could mutate it. It's also not a perfect inference, just a conservative one
[1]The pusca README claims that it can infer impurity of methods whose last expression is a call to an impure method. I'm not quite sure how it can do this, as checking if that last expression is an impure call requires checking if it's calling a not-declared-impure method that should be declared impure by this rule, and the implementation might not be available to the compiler at that point (and indeed could be changed later even if it is). But all I've done is look at the README and think about it for a few minutes, so I might be missing something.

Why the naming: NodeList vs childNodes

I have been wondering about a stupid thing about the DOM. Why do the standards define NodeList with the postfix List to make it clear it is an array while have a some properties or functions like childNodes or getElementsByTagName which use the postfix letter s?
I find it contradictory when the standards define members with different suffixes for the same purpose (to describe an array).
Edit: It actually seems that NodeList is not even an array. Does this explain this?
NodeList is an interface.
childNodes is a member of the Node interface.
getElementsByTagName is a member of the Document interface.
BTW, both these members return a value of type NodeList.
So, there is a difference: one is an interface, and the other two are members of interfaces.