Get modifiers from class - rascal

I'm trying to get all the modifiers from a class in Rascal.
m = createM3FromEclipseProject(projectLocation);
for(cl <- classes(m)){
set[Modifier] modi = { f | f <- m#modifiers[cl], f.scheme == "java+class"};
println(modi);
}
This gives me an empty set of modifiers for all classes, but if I check m#modifiers, it is not empty.
So m#modifiers[cl] won't give me the modifiers that belong to that the class with location cl. How do I get these modifiers?

Something's wrong with the code you presented. If you try to do it step by step on the REPL you will see why:
For an example project with Fruit and such extracted into the M3 model m I have this:
rascal>classes(m)
set[loc]: {
|java+class:///Fruit|,
|java+class:///Apple|,
|java+class:///HelloWorld|
}
rascal>m#modifiers
rel[loc definition,Modifier modifier]: {
<|java+interface:///IFruit|,public()>,
<|java+method:///HelloWorld/main(java.lang.String%5B%5D)|,static()>,
<|java+class:///Fruit|,abstract()>,
<|java+method:///Apple/edible()|,public()>,
<|java+method:///Fruit/edible()|,public()>,
<|java+class:///Apple|,public()>,
<|java+method:///HelloWorld/main(java.lang.String%5B%5D)|,public()>,
<|java+class:///HelloWorld|,public()>,
<|java+method:///Fruit/edible()|,abstract()>,
<|java+class:///Fruit|,public()>
}
So m#modifiers[someClass] returns a set of modifiers:
rascal>m#modifiers[|java+class:///Fruit|]
set[Modifier]: {
abstract(),
public()
}
In your code f <- m#modifiers[cl] the f thus binds to a modifier and not to a source location. Somehow the code does not throw an exception but rather lets the condition fail for you? Because I get this result instead:
rascal>{ f | cl <- classes(m), f <- m#modifiers[cl], f.scheme == "java+class"};
|prompt:///|(46,1,<1,46>,<1,47>): Undeclared field: scheme for Modifier
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UndeclaredField/UndeclaredField.html|
If you want to print the modifiers for each class, then this code should do it:
for (cl <- classes(m)) {
println("modifiers for <cl> are <m#modifiers[cl]>");
}

Related

Why is `functionArgs` implemented twice? (i.e, as a primop and in `lib`)

Trying to understand callPackage, so looked up its implementation where it uses lib.functionArgs (source), but there is already a builtins.functionArgs primop, an alias of __functionArgs (implemented in C).
lib.functionArgs is defined as
/* Extract the expected function arguments from a function.
This works both with nix-native { a, b ? foo, ... }: style
functions and functions with args set with 'setFunctionArgs'. It
has the same return type and semantics as builtins.functionArgs.
setFunctionArgs : (a → b) → Map String Bool.
*/
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
and the __functionArgs attribute above is coming from setFunctionArgs (source):
/* Add metadata about expected function arguments to a function.
The metadata should match the format given by
builtins.functionArgs, i.e. a set from expected argument to a bool
representing whether that argument has a default or not.
setFunctionArgs : (a → b) → Map String Bool → (a → b)
This function is necessary because you can't dynamically create a
function of the { a, b ? foo, ... }: format, but some facilities
like callPackage expect to be able to query expected arguments.
*/
setFunctionArgs = f: args:
{
__functor = self: f;
__functionArgs = args;
};
I understand what setFunctionArgs does, and the comment above its declaration tells why it is necessary, but I can't understand it; both clauses of that sentence are clear but not sure how the first statement prevents the second one to be achieved (without setFunctionArgs, that is).
danbst also tried to elucidate this further,
lib.nix adds __functionArgs attr to mimic __functionArgs builtin. It
used to "pass" actual __functionArgs result down to consumers, because
builtin __functionArgs only works on top-most function args
but not sure what the "consumers" are, and couldn't unpack the last clause (i.e., "builtin __functionArgs only works on top-most function args"). Is this a reference to the fact that Nix functions are curried, and
nix-repl> g = a: { b, c }: "lofa"
nix-repl> builtins.functionArgs g
{ }
?
lib.functionArgs also doesn't solve this problem, but I'm probably off the tracks at this point.
Notes to self
__functor is documented in the Nix manual under Sets.
$ nix repl '<nixpkgs>'
Welcome to Nix version 2.3.6. Type :? for help.
Loading '<nixpkgs>'...
Added 11530 variables.
nix-repl> f = { a ? 7, b }: a + b
nix-repl> set_f = lib.setFunctionArgs f { b = 9; }
nix-repl> set_f
{ __functionArgs = { ... }; __functor = «lambda # /nix/store/16blhmppp9k6apz41gjlgr0arp88awyb-nixos-20.03.3258.86fa45b0ff1/nixos/lib/trivial.nix:318:19»; }
nix-repl> set_f.__functionArgs
{ b = 9; }
nix-repl> set_f set_f.__functionArgs
16
nix-repl> set_f { a = 27; b = 9; }
36
lib.functionArgs wraps builtins.functionArgs in order to provide reflective access to generic functions.
This supports reflection with builtins.functionArgs:
f = { a, b, c }: #...
Now consider the eta abstraction of the same function:
f' = attrs: f attrs
This does not support reflection with builtins.functionArgs. With setFunctionArgs, you can restore that information, as long as you also use lib.functionArgs.
I recommend to avoid reflection because everything that I've seen implemented with it can be implemented without it. It expands the definition of a function to include what should normally be considered implementation details.
Anyway, the primary motivation seems to be callPackage, which can be implemented with normal attrset operations if you change all packages to add ... as in { lib, stdenv, ... }:. I do have a morbid interest in this misfeature that is function reflection, so if anyone finds another use case, please comment.

F# Define function to act as delegate to .net Action

in System.Activities.WorkflowApplication there is a delegate property:
public Action<WorkflowApplicationCompletedEventArgs> Completed { get; set; }
In my program so far, I have a variable that is an instance of this class
I want to define an F# function to set that:
let f (e: WorkflowApplicationCompletedEventArgs) =
// body
myInst.Completed <- f
but this produces the error:
Error 102 This expression was expected to have type
Action but here has type
'a -> unit
how do I complete function "f" to satisfy the compiler?
If you pass an anonymous function fun a -> ... to a method or a constructor that expects a System.Action<...> or a System.Func<...>, then it is automatically converted; in any other case, you need to convert it explicitly like #Funk indicated.
let f = System.Action<WorkflowApplicationCompletedEventArgs>(fun e ->
// body
)
myInst.Completed <- f
// Another solution:
let f (e: WorkflowApplicationCompletedEventArgs) =
// body
myInst.Completed <- System.Action<_>(f)

How to sort a list of different type objects by timestamp

New to F#
Have a list of objects.
Objects share the same base class, but the timestamp attribute we want to order by is not present on the base class.
Every object in the list will have the timestamp attribute.
Requirement is to order the objects by the timestamp descending.
Attempted
let sortedList = unsortedList.Sort();
This results in
System.InvalidOperationException: Failed to compare two elements in
the array. ---> System.ArgumentException: At least one object must
implement IComparable. at System.Collections.Comparer.Compare(Object
a, Object b)
You didn't post code so I can't give you the exact code to solve it.
Typically you will have a property in the Base Class or alternatively in an Interface, but let's suppose you have this:
type A() = class end
type B(x) =
inherit A()
member val timestamp = x with get, set
type C(x) =
inherit A()
member val timestamp = x with get, set
let lst = [ new B(5) :> A ; new C(15) :> A; new B(4) :> A ;]
And you can't touch that code. Then what you can do is this:
let getTimeStamp (x:A) =
match x with
| :? B as x -> x.timestamp
| :? C as x -> x.timestamp
| _ -> failwith "subtype not handled"
lst |> List.sortBy (fun x -> -getTimeStamp x)
Using reflection is another possibility (see Mau's answer).
If you post a code sample I can give you a more specific solution and test it.
You can access the timestamp property using the dynamic operator ? as shown here:
C#'s 'dynamic' in F#

How to implement variable arguments in F#

I want to implement a F# function which may accept 1 or 2 arguments. I would like to use the function like this:
let foo = ...
foo "a"
foo "a" "b"
Both the arguments can be the same type. I read the pages about match pattern, active pattern, but cannot find one works for me.
I believe this is due to some of the underlying .Net features, but I think you have to use a class with overloaded methods - something like
type t() =
static member foo a = "one arg"
static member foo (a,b) = "two args"
On a type member, you can use optional params:
type Helper private () =
static member foo (input1, ?input2) =
let input2 = defaultArg input2 "b"
input1, input2
To call this method:
Helper.foo("a")
Helper.foo("a", "b")
Is this what you're after?
You can't use optional params on a function though, unfortunately.
In addition to the other answers, here are a few more "almost solutions". They are not strictly what you wanted, but are worth knowing anyway.
Using a list (or an array) and pattern matching:
let f = function
| [a, b] -> ...
| [a] -> ...
| [] -> failwith "too few arguments"
| _ -> failwith "too many arguments"
f ["a"]
f ["a" ; "b"]
Problems: parameters are not named, not clear from function signature how many parameters it takes.
Using a record to pass all optional parameters:
type FParams = { a : string; b : string }
let fdefault = { a = "a" ; b = "b" }
let f (pars: FParams) = ...
f { fdefault with b = "c" }
Problem: a is also optional, which is not what you wanted. Can be useful though.
In addition to the other answers, you might also be able to do what you want via partial application and currying. Like this:
let foo a b =
a + b
let foo2 a =
foo 1 a;;
Obviously you'd want to fix the first parameter in the call to foo within foo2 to whatever default you want.

Is it possible to use the pipeline operator to call a method on a returned object?

Is it possible to call a method on a returned object using the pipeline infix operator?
Example, I have a .Net class (Class1) with a method (Method1). I can currently code it like this:
let myclass = new Class1()
let val = myclass.Method1()
I know I could also code it as such
let val = new Class1().Method1()
However I would like to do be able to pipeline it (I am using the ? below where I don't know what to do):
new Class1()
|> ?.Method1()
Furthermore, say I had a method which returns an object, and I want to only reference it if that method didn't return null (otherwise bail?)
new Class1()
|> ?.Method1()
|> ?? ?.Method2()
Or to make it clearer, here is some C# code:
public void foo()
{
var myclass = new Class1();
Class2 class2 = myclass.Method1();
if (class2 == null)
{
return;
}
class2.Method2();
}
You can define something similar to your (??) operator fairly easily (but operators can't start with a question mark):
let (~??) f x =
if (x <> null) then
f x
Unfortunately, your pipelined code will need to be a bit more verbose (also, note that you can drop the new keyword for calling constructors):
Class1()
|> fun x -> x.Method1()
Putting it all together:
Class1()
|> fun x -> x.Method1()
|> ~?? (fun x -> x.Method2())
Using a custom operator as 'kvb' suggests is definitely an option. Another approach that you may find interesting in this case is to define your own 'computation expression' that automatically performs the check for null value at every point you specify. The code that uses it would look like this:
open System.Windows.Forms
// this function returns (0) null, or (1) btn whose parent is
// null or (2) button whose parent is not null
let test = function
| 1 -> new Button(Text = "Button")
| 2 -> new Button(Text = "Button", Parent = new Button(Text = "Parent"))
| _ -> null
let res =
safe { let! btn = test(2) // specify number here for testing
// if btn = null, this part of the computation will not execute
// and the computation expression immediately returns null
printfn "Text = %s" btn.Text
let! parent = btn.Parent // safe access to parent
printfn "Parent = %s" parent.Text // will never be null!
return parent }
As you can see, when you want to use a value that can potentially be 'null', you use let! inside the computation expression. The computation expression can be defined so that it immediately returns null if the value is null and runs the rest of the computation otherwise. Here is the code:
type SafeNullBuilder() =
member x.Return(v) = v
member x.Bind(v, f) =
if v = null then null else f(v)
let safe = new SafeNullBuilder()
BTW: If you want to learn more about this, it is very similar to 'Maybe' monad in Haskell (or computation working with F# option type).

Resources