What is the point of type definition on method parameters on Caché Object (from Intersystems) since after it's pre-compiled to the .int format, it removes any typing information, thus making no difference at all?
Those types aren't used/checked internal to Cache code, but they are used when you expose your classes via XML, SQL, etc.
One would hope that in a future version Intersystems would start doing some compile-time type checking, but that may be too much to ask.
If you're writing ANSI M code, you shouldn't have types at all. My guess is that this is specific to Intersystems code.
There aren't really datatypes in Cache, so there is no type checking.
Related
Reviewing a question recently but could not post response due to lacking any reputation.
In this question regarding a compiletime error coming from using List<Map<String,Object>> there was a compile time error when trying to pull the value of the Object which was known to be either a String or a Widget.
My resolution was to use as when calling using the values 'as String' or 'as Widget' in the appropriate spots.
Another more elegant solution was to replace 'Object' with 'dynamic'.
I remember reading 'as' was discouraged where possible. I don't know why, and I feel it resolve the issue. Is this simply because it should be cast as a specific type when created?
When trying to recreate in Dartpad I had no issues, potentially just a flutter issue?
Why does dynamic work, but Object doesn't in this scenario? I mean everything will be a subtype of object right?
Thanks,
Can copy and paste code across if required, felt context of attached question was valuable.
I remember reading 'as' was discouraged where possible. I don't know why, and I feel it resolve the issue. Is this simply because it should be cast as a specific type when created? When trying to recreate in Dartpad I had no issues, potentially just a flutter issue?
Explicit type casts are a code smell. They're not necessarily wrong or bad, but they're frequently indicative of APIs that are unnecessarily awkward and perhaps could be designed better to use the correct types in the first place.
Explicit casts are also brittle. They introduce potential runtime failure points. That is, if the actual (runtime) type of the object changed, the cast could fail, and that failure wouldn't be noticed at compilation time.
Why does dynamic work, but Object doesn't in this scenario? I mean everything will be a subtype of object right?
dynamic is a special type that disables static (compile-time) type-checking. Object (like every other non-dynamic type) is statically type-checked, so any methods that you call on it must be statically known to exist on that type.
Note that using dynamic is brittle too since failures involving dynamic types are inherently runtime failures. Using dynamic also can be less readable in general; except when there's some obvious context, readers won't know what type you expect the object to be and therefore won't know what behavior you expect the called method to have.
Also see the Effective Dart recommendation: AVOID using dynamic unless you want to disable static checking.
I have read various old StackOverflow discussions on this general topic but there is still one part of the puzzle which appears, to me at least, to be missing.
It is simply this: what is the actual mechanism by which the anonymous function is serialized? And, where could we find its source code?
Or is it all just magic?
Other relevant SO articles (the third of these itself points to some useful articles outside StockOverflow):
Serialization of Scala Functions
Why Scala can serialize...
How to serialize functions in Scala
I'm going to answer my own question with what, I believe is the correct answer. The reason I'm doing it this way is that it seems to me that this aspect of serialization is never explained and it does appear to work just by magic. I essentially confirmed (to my satisfaction) the answer as part of the research I was doing to ensure that my question above was indeed appropriate.
But the main reason I'm offering my own answer is that I invite knowledgeable users either to agree with it, to correct it, to expand upon it, or to destroy it. Here goes...
It's all magic. No, I'm just kidding. But essentially the mechanism, once Scala has taken the step of representing the anonymous function as a Class, is entirely provided for by Java. In addition, we, the programmer, need to ensure that an anonymous function is as much pure code as possible: no references to any objects that might not be serializable. The secret sauce is to be found in the Java class: ObjectStreamClass. Which, in turn, is invoked by the Java serialization classes: ObjectInputStream and ObjectOutputStream.
Essentially the serialized bytes contain the full pathname of the class, its serialVersionUID, and whatever other relevant information is necessary. When deserializing, the system will simply look up the class in the appropriate classpath and return a reference to it. This obviously assumes that the deserializing system has the class in its classpath. The mechanism for that is a little beyond the scope of my research but it's clear that in a system like Spark, it should be easy to arrange.
No (additional) compilation/decompilation of byte code is necessary as the classLoader has everything necessary. I'm slightly surprised to find the ObjectStreamClass in java.io rather than in the reflection package, but I suppose there's an argument for it being there, given the tight coupling with ObjectInputStream and ObjectOutputStream.
One thing to keep in mind is that while we think in terms of serializing/deserializing objects, rather than classes, what we are dealing with here is an object of type Class.
One more thing to note is that in Scala 2.12, anonymous functions are now implemented differently: as Java8 lambdas. This has broken the mechanism described above in a rather serious way. So serious, that Spark is currently having trouble supporting Scala 2.12. The holdup appears to be this issue: SPARK-14540.
I have a relatively large Scala code base that does not use named parameters for any function/class calls. Rather than going in and manually entering it, which would be a very tedious process, I was looking at a formatter to do the job. The best I found is scalariform, but I'm not sure whether I can even write a rule for something so complex.
I'm curious if anyone has ran into a similar problem and found a powerful formatter.
The Scala Refactoring library might be something you could use. You will need some knowledge of Scala's Abstract Syntax Tree representation.
Why do you want to use named parameters throughout your code base? I like IntelliJ's default which is to suggest to name boolean arguments (only).
How do they perform auto complete of code in eclipse or other ides? What is basic principle behind it?
You know how you have to explicitly attach source code to non-standard libraries you imported in Eclipse? When you do that, text-search index is built over that source and this way IDE knows to offer you auto-complete feature. Roughly, I suppose it is something as associative array where key is the prefix of method you typed, and value is description of that method.
Now what is important for this functionality is to be implemented efficiently regarding both time and memory consumption. It would be very inefficient to store the same entry for every possible prefix of some method. (Or even to store every prefix!)
One of interesting structures that could be suitable for this problem is Trie, which is inherently optimized for prefix search while keeping acceptable memory usage.
Take a look here for a simple example:
http://www.sarathlakshman.com/2011/03/03/implementing-autocomplete-with-trie-data-structure/
Besides Tries, used for the case when you have already typed the beginning of the name of a method/var, I think it also uses some sort of type comparison/analysis for the case when you try to invoke a method and the IDE suggests you a local/global variable to pass as parameter to that method call.
trait PublicApi{
def sayHi(from:String,content:String)
}
I know that it is impossible in java to get "from" and "content" in runtime
but can scala manifest help me out here?? like defining
trait PublicApi{
def sayHi(from:String,content:String)(m:Manifest)
}
invoking m.methodErasure.getArgumentName(0) would return "from"
In its current implementation, ClassManifest is all about type reification, and would know nothing about parameter names.
Unless you:
add meta data for parameter names at compile time
or use a cut down version of ASM (Java bytecode manipulation and analysis framework) to extract debug information from a class at runtime
, as paranamer does (like Monkey mentions in the comments), you won't have any information on the parameter name (at least not before Java 8.0, even though it was initially mentioned for Java6!: the proposal is still in progress)
The Scala "signature" DOES contain these parameter names, as you rightly surmise. After all, they have to be stored somewhere for named/default parameters to work. You can also use scalap to see these names.
Feel free to take a look at my preliminary work on a reflection library.
It's a work in progress, and I make absolutely no guarantees as to the current feature set or correctness, but may give you some idea as to what's involved - you might even be able to use it for extracting parameter names in its current state. This is also something I'm actively working on, so you can expect it to improve with the passage of time...