Let's say I'm writing a swift module, and I want to name a type with a name which already exists. If the name is from another module, it's easy. I can just use the module name as a namespace:
import Foundation
class MyClass {
class Notification : Foundation.Notification { ... }
}
My question is, is there any way to do the same with types in the same module? For example, I would like to be able to do something like this:
class Notification { ... }
class MyClass {
class Notification : Module.Notification { ... }
}
Where Module.Notification is a reference to the type declared above. Is such a thing possible?
You need to use the actual name of your module:
class Notification { ... }
class MyClass {
class Notification : MyAmazingTwitterApp.Notification { ... }
}
If you're working in Xcode, this defaults to your target name. There's a build setting "Product Module Name", under "Packaging" that lets you change this.
If you're using the Swift build system, this is of course specified in your manifest file, via the PackageDescription.
Related
I have a code, and I want to make sure that a class definion is indeed loaded.
Lets say that the class name is foo. I can do the following:
if { [ catch { foo new } ] } { source "path_to_code" }
Is there a way to do it without catch?
You can use the info command:
if {![info object isa class foo]} {source "path_to_code"}
Let me be more specific here: This is used in Unity 2017 so the syntax they are using is this:
class CameraMotionBlurEditor extends Editor
{
var preview : SerializedProperty;
var previewScale : SerializedProperty;
...
function OnInspectorGUI () {
if (preview.boolValue) dosomething()
}
}
What I'm getting errors in is this preview.boolValue reference.. it claims it's ambiguous so therefore whatever this class is extending, must also have a declaration of that variable name. What I don't know is how to specify the local one.
The this keyword is used to refer to the current instance of the class. Retrieving the preview.boolValue from the current instance of the class hence becomes this.preview.boolValue:
function OnInspectorGUI () {
if (this.preview.boolValue) dosomething()
}
Note that UnityScript is slowly becoming deprecated, and the recommended route of action is to instead program Unity scripts in C#.
I cannot access one class from another. Classes are defined in different files.
#foo.ps1
Class Foo { static [string]$data }
#bar.ps1
using module ".\foo.ps1"
class Bar {
Bar() {
[Foo]::data="test" #not working
}
}
[Foo]::data="test" #works
Posting as an answer so this doesn't go unanswered (credit to PetSerAl).
Rename foo.ps1 to foo.psm1 so that PowerShell knows it's a module.
If you run the following code in the Swift REPL, it will print out main.SomeClass. Here is a Swiftstub to try it out: http://swiftstub.com/887338044
class SomeClass {
func doesSomething() {
print(self) // prints "main.SomeClass
}
}
let someClass = SomeClass()
someClass.doesSomething()
Is it possible to get access to main the object/variable/constant to inspect it? What is main?
You are always in a module (namespace). In an iOS app, it's the app, and it has the name of the project by default (you can change that in the build settings). In the REPL, we have to make something up, so the module is called main. It isn't an "object", "variable", or "constant" that you can "access"; it's just the namespace. Your class's real name simply is main.SomeClass.
I have some TypeScript code that is being generated by a tool. I'd like to extend this class in another file. As of 0.9.1.1, what's the best way to go about this?
I thought maybe I could staple my additional functions onto the prototype, but this is giving various errors (which change depending what mood the compiler is in).
For example:
Foo.ts (generated by a tool)
module MyModule {
export class Dog { }
}
Bar.ts
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
You cannot split a class definition between multiple files in TypeScript. However typescript understands how JavaScript works and will let you write idomatic JavaScript classes just fine:
module MyModule {
export function Dog(){};
}
module MyModule {
function bark(): string {return 'woof';}
Dog.prototype.bark = bark;
}
Try it online
One way around this is to use inheritance:
class BigDog extends Dog{
bark(){}
}
I have encountered your problem as well before, but I had some deeper problems. You can see from basarat's example, that simple functions can be added as an extension to the prototype, but when it comes to static functions, or other static values you might want to extend your (presumably third party) class, then the TSC will warn you, that there is no such method defined on the class statically.
My workaround was the following little hack:
module MyModule {
export function Dog(){};
}
// in the other file
if (typeof MyModule !== 'undefined'){
Cast<any>(MyModule.Dog).Create = ()=>{return new Dog();};
}
// where Cast is a hack, for TS to forcefully cast types :)
Cast<T>(element:any):T{ return element; }
This should cast MyModule.Dog, to an any object, therefore allowing attachment of any kinds of properties, functions.