I noticed today while working on a very small JS file that my defined type wasn't allowing intellisense when a variable was declared later on as being of that type. Turns out that changing the typedef to have a capital letter fixes the issue. My type definition is at the top of the file and the variable that is of that type is within an IIFE.
If I move the type definition inside the IIFE, then it works no matter the case of the type's name. However, leaving the type definition at the top of the file (outside the IIFE) and making the name capitalized also makes it work.
Is it documented anywhere that a capitalized type definition makes it global?
EDIT: Adding a couple screenshots. This seems to be sporadic to reproduce using simple examples.
Non-working
Working
EDIT_2: It seems to be related to having a variable with the exact same name as the type definition.
/**
* An object that stores all the necessary contextual data
* Defined inside the HTML file loaded for the alert
* #typedef {Object} Test
* #prop {Number} personId person_id of the patient
*/
(function() {
/** #type {Test} */
const Test = window.Test;
Test
})();
You should consider the #typedef as being a variable – one that's only usable in your type declarations.
That means that you can override that variable in your local scope, so that it means something else there.
And that's exactly what you are doing here.
Related
So I've searched and found a lot on this topic, but nothing that seems to directly address my question or if it does it either pretty much requires me to basically reimplement my object definition in JSDoc (eg #typedef, which strikes me as duplication of effort and makes two places to maintain 'code') or does it in a way that produces weird documentation (eg, using namespaces).
So if I have a classes with a nested object (say, a dictionary lookup), I can't seem to get its members to appear automagically in JSDoc as part of the class documentation. Instead, they all appear as individual entries in the Global section.
Example:
/**
* An example class
* #class
*/
class MyClass {
/** An object literal */
myObjectLiteral = {
/** property 1, ya know? */
objProperty1: "moo",
/** property 2, baby! */
objProperty2: "goo"
}
}
MyClass appears as a class with myObjectLiteral as a member, but objProperty1 and objProperty2 appear as entries in the Global section.
Is there an elegant way to get JSDoc to include the object literal and its properties in the class documentation? I thought that using #memberof might do the trick, but it didn't seem to work (tho I may have effed up the syntax).
I have some simple code from:
https://www.codeguru.com/csharp/csharp/cs_misc/dllsandexecutables/article.php/c4239/Creating-and-Using-C-DLLs.htm
which generates a dll to do some simple math. I wanted to add a subclass
namespace MathFunctions
{
public class Add : MultiClass
{
public static int MultiplyAndAdd(int a, int b, int c)
{
return (a * b) + c;
}
}
}
Then call it from powershell.
Running powershell against the master classes returns data back without an issue
Import-module("C:\temp\MathFunctions.dll")
[MathFunctions.MultiClass]::Multiply(10, 2)
returns 20 as expected, but I can't figure the format to access the subclass. I've tried variations on:
[MathFunctions.MultiClass.Add]::MultiplyAndAdd(10, 2, 3)
[MathFunctions.MultiClass+Add]::MultiplyAndAdd(10, 2, 3)
[MathFunctions.MultiClass]:Add:MultiplyAndAdd(10, 2, 3)
[MathFunctions.MultiClass]::Add.MultiplyAndAdd(10, 2, 3)
but I always get variations on
Unable to find type [MathFunctions.MultiClass.Add]
I've also looked for the method in powershell via:
[MathFunctions.MultiClass] | get-member -MemberType method
but my subclass isn't listed.
I know I'm accessing it incorrectly. I can't figure out how to access the subclass from powershell.
I'm fairly sure subclasses can be accessed, as the closest example is:
PowerShell IComparable with subclasses
but I don't see how he aliased it.
Thanks
Thank you bluuf who pointed me in the right direction.
I ended up using dotPeek to take the dll apart (now it was public [blush]).
Code is as above and the powershell method is just the subclass name:
[MathFunctions.Add]::MultiplyAndAdd(5,2,3)
which gave an answer - not an error
To explain the confusion in more detail:
You mistakenly thought that class-inheritance relationships must be reflected in a given class' full (namespace-qualified) type name, so you thought that because class Add derives from class MultiClass, MultiClass too must be reflected in the full type name.
In reality, a class' derivation is irrelevant with respect to its full name; all that matter is the namespace in which it is placed.
In other words: to form the full type name for any type (class), use <EnclosingNamespace>.<TypeName>[1], which in your case means:
MathFunctions.Add
Using that as a PowerShell type literal - [MathFunctions.Add] - allows you to access the static MultiplyAndAdd() method via ::, the static-member access operator, as shown in your own answer.
Also, remember that tab-completion can be helpful here, because it works with type names too; in your case, typing [Add<tab> will expand to [MathFunctions.Add, yielding the full type name.
(If multiple available (public) type names start with Add, you may to have to press the tab key repeatedly to cycle through the matches.)
[1] A variation is required to access a nested class, i.e., a class embedded in another class:
<EnclosingNamespace>.<EnclosingTypeName>+<NestedTypeName>, e.g., if your Add class had a nested class named Inner: MathFunctions.Add+Inner
This is on a large code base, so I'm just looking for general pointers.
In one file, a function takes a parameter a:MyClass.
At runtime, typeof a.b yields string.
In VSCode I hit F12 on the b of a.b and am brought (correctly, judging by the import statement) to another file:
export class MyClass {
...
b: string[]; // brought to this line
}
How is it possible within a TypeScript environment for a.b to be a string instead of a string[] like the class declaration says? And what should I look for that might cause this behaviour?
Type declarations in TypeScript are only suggestive. Because TypeScript is transpiled into plain JavaScript it can not make any guarantees about the actual content of a variable.
Even in TypeScript itself is fairly easy to put an object of a different type in a variable:
let myClass = new MyClass();
myClass.b = "I'm a string placed into a string array" as any;
Notice the as any at the end of the last line, this removes type information from an expression and allows it to be placed into a variable or argument of any type.
I have two methods on my companion object (model.Product):
def apply(p:ProductSyntax)(rs: WrappedResultSet): Product
def apply(p: ResultName[Product])(rs: WrappedResultSet): Product
The first method delegates to the second and I would like to indicate this in the docs. I tried using:
/**
* delegates to [[apply]]
* /
But scaladoc complains that this is ambiguous but tells me that
(p: scalikejdbc.ResultName[model.Product])(rs: scalikejdbc.WrappedResultSet): model.Product in object Product
is an option
However I can't work out how to tell scaladoc to use this method. I tried
/**
* Delegates to [[apply(scalikejdbc.ResultName[model.Product])(scalikejdbc.WrappedResultSet):model.Product]]
* /
But it tells me that no member is found:
Could not find any member to link for "apply(scalikejdbc.ResultName[model.Product])(scalikejdbc.WrappedResultSet):model.Product".
How would I add a link to the def apply(p: ResultName[Product])(rs: WrappedResultSet): Product method?
So this is what I discovered:
Everything must be fully qualified, even the class/object itself
Package dots should be escaped with \
You cannot use any spaces in the signature
Paramaters should include the name not just the type i.e. foo(a:String) not foo(String)
The signature should end with a *
Finally this worked:
[[apply(p:scalikejdbc\.ResultName[model\.Product])(rs:scalikejdbc\.WrappedResultSet):model\.Product*]]
HOWEVER ... the backslash escaping and * also appears in the generated html!
What is the difference between
class Test {
private[this] val foo = 0
}
vs
class Test {
private val foo = 0
}
What all can go inside the []? Also, what should I search for when I want to look up the specs of this? I tried Googling various combinations of "scala access modifier arguments/parametrized scala access modifier" and nothing came up.
what should I search for when I want to look up the specs of this?
In The Scala Language Specification it is defined as "access modifier" and "access qualifier" (see BNF in §5.2).
What is the difference between
...
What all can go inside the []?
You can put class name, package name or this there. Here is a relevant quote from language specs that explains this (see §5.2 for more details):
The modifier can be qualified with an identifier C (e.g. private[C ]) that must
denote a class or package enclosing the definition. Members labeled with
such a modifier are accessible respectively only from code inside the package
C or only from code inside the class C and its companion module (§5.4).
An different form of qualification is private[this]. A member M marked
with this modifier is called object-protected; it can be accessed only from
within the object in which it is defined. That is, a selection p.M is only legal if the prefix is this or O.this, for some class O enclosing the reference. In
addition, the restrictions for unqualified private apply.
The first one is private for instance class, second is for class. If you use second version you have access from another instance of Test class (it's usefull for equals method or similiar).