Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Moose 11 #16

Closed
2 tasks done
fuhrmanator opened this issue Feb 23, 2024 · 7 comments
Closed
2 tasks done

Port to Moose 11 #16

fuhrmanator opened this issue Feb 23, 2024 · 7 comments

Comments

@fuhrmanator
Copy link
Owner

fuhrmanator commented Feb 23, 2024

Pharo 12 will soon be finalized, and so Moose 11 should be stable soon as well. This means the FamixTypeScript MM should work in Moose 11.

Currently, here are some of the problems:

  • TParameterType in Moose 11 doesn't exist, and so the MM can't be generated, for example (in defineHierarchy):

        parameterType --|> type.
        parameterType --|> #TParameterType.

    There are surely hints in the FamixJava metamodel in Moose 11 ;-) So far, I've found:

    • there are classes (without traits) ParametricClass, ParameterType, ParametricInterface, ParametricMethod, and ParameterConcretization (perhaps unrelated but seems like it's important).
  • At least one class in Extensions has a dependency on a class that's generated in Entities. When I try to load Extensions, I get the following Warning (Maybe this can be fixed in the Baseline? Maybe this is just a stricter warning in Pharo 11?):

    Warning: Package *Famix-TypeScript-Extensions depends on the following classes:
    FamixTypeScriptParameterizableClass
    FamixTypeScriptParameterType
    FamixTypeScriptType
    FamixTypeScriptClass
    You must resolve these dependencies before you will be able to load these definitions:
    FamixTypeScriptClass>>#accessedAttributes
    FamixTypeScriptClass>>#isInterface
    FamixTypeScriptClass>>#isInterface:
    FamixTypeScriptClass>>#methodsWithoutSutbsAndConstructors
    FamixTypeScriptParameterType>>#isParameterType
    FamixTypeScriptParameterizableClass>>#parameters
    FamixTypeScriptParameterizableClass>>#parameters:
    FamixTypeScriptType>>#isInterface
    FamixTypeScriptType>>#isParameterType

@fuhrmanator
Copy link
Owner Author

@NicolasAnquetil Feel free to add items you think will help here :)

@fuhrmanator
Copy link
Owner Author

To gain insights into the changes in Famix (Traits, etc.) for Moose 11, here's a PlantUML class diagram of the Moose 11 version of FamixJava metamodel, generated with the following script in a Playground:

FamixUMLDocumentor new
  model: FamixJavaModel ;
  generate ;
  exportWith: (FamixUMLPlantUMLBackend new).

@fuhrmanator
Copy link
Owner Author

Here are some examples with Generics:

Generic Function

A generic function can accept parameters of any type and return a value of the same type:

function identity<T>(arg: T): T {
    return arg;
}

let numberIdentity = identity<number>(42); // numberIdentity is of type number
let stringIdentity = identity<string>("Hello, TypeScript!"); // stringIdentity is of type string
let arrayIdentity = identity<number[]>([1, 2, 3]); // arrayIdentity is of type number[]

Generic Interface

A generic interface can define a structure that works with any type:

interface GenericIdentityFn<T> {
    (arg: T): T;
}

function identityFn<T>(arg: T): T {
    return arg;
}

let myIdentity: GenericIdentityFn<number> = identityFn;
console.log(myIdentity(10)); // Outputs: 10

Generic Class

A generic class can create instances that operate on various types:

class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;

    constructor(zeroValue: T, add: (x: T, y: T) => T) {
        this.zeroValue = zeroValue;
        this.add = add;
    }
}

let myGenericNumber = new GenericNumber<number>(0, (x, y) => x + y);
console.log(myGenericNumber.add(5, 10)); // Outputs: 15

let stringNumeric = new GenericNumber<string>("", (x, y) => x + y);
console.log(stringNumeric.add("Hello, ", "world!")); // Outputs: Hello, world!

Generic Constraints

You can add constraints to generics to restrict the kinds of types you can use:

interface Lengthwise {
    length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
    console.log(arg.length); // Now we know it has a .length property, so no error
    return arg;
}

loggingIdentity({ length: 10, value: "Hello" }); // Works because the argument has a length property
// loggingIdentity(3); // Error: Argument of type 'number' is not assignable to parameter of type 'Lengthwise'.

Generic Types in Arrow Functions

You can also use generics with arrow functions:

const identity = <T>(arg: T): T => {
    return arg;
};

let output = identity<string>("Hello, TypeScript!"); // output is of type string
console.log(output); // Outputs: Hello, TypeScript!

Using Multiple Type Variables

You can define a function or class with multiple type variables:

function map<K, V>(array: K[], fn: (item: K) => V): V[] {
    return array.map(fn);
}

let stringToNumber = map<string, number>(["1", "2", "3"], (str) => parseInt(str));
console.log(stringToNumber); // Outputs: [1, 2, 3]

@fuhrmanator
Copy link
Owner Author

Currently, ArrowFunction is not in the metamodel. #22

@fuhrmanator
Copy link
Owner Author

See moosetechnology/Famix#773 (comment) as well.

@fuhrmanator
Copy link
Owner Author

https://modularmoose.org/moose-wiki/Developers/predefinedEntities#genericity helps with understanding Generics in Java.

@fuhrmanator
Copy link
Owner Author

fixed in #18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant