What is the equivalent clojure class to the type 'int' in java?
(def int-class
(->>
(seq (.getMethods java.util.Date))
(filter #(= "setDate" (.getName %)))
first
(.getParameterTypes)
first)))
int-class => int
(= int-class int) => false
It's spelled Integer/TYPE, though Clojure prints it out as int. TYPE here is a static member of the Integer class; the type of this static member is Class. There are analogous static members in the other primitive-wrapping classes.
When found in code, the symbol int typically resolves to the Var #'clojure.core/int (which holds the function for coercing to int).
Related
What does the double main mean? Are there two nested levels of main scope somehow?
Error: cannot implicitly convert expression myFunction(f) of type main.M!(Tuple!(wstring, wstring)*) to main.main.M!(Tuple!(wstring, wstring)*)
The only difference is main.main instead of main.
Error: cannot implicitly convert expression myFunction(f) of type
main.M!(Tuple!(wstring, wstring)*)
to
main.main.M!(Tuple!(wstring, wstring)*)
Context:
M is a struct defined at the top level
the call to myFunction(f) is inside a delegate literal that is immediately called; something like (delegate bool () {myFunction(f);return true;})()
Turns out I had two copies of the struct definition.
M struct definition
definition of myFunction which returns values of type M
M struct definition, again
M = myFunction(f);
thus we have a type mismatch, since the two M's are actually different types, but under the same name
I'm playing around with Ceylon and I'm trying to create an alias for a tuple. The following do not work:
class MyPair(Integer i, Float f) => [i, f];
class MyPair(Integer i, Float f) => [Integer, Float](i, f);
class MyPair(Integer i, Float f) =>
Tuple<Integer|Float, Integer, Tuple<Float, Float, Empty>>(i, [f]);
class MyPair(Integer i, Float f) =>
Tuple<Integer|Float, Integer, Tuple<Integer|Float, Float, Empty>>(i, [f]);
class MyPair(Integer i, Float f) =>
Tuple<Integer|Float,Integer,Tuple<Float,Float,Empty>>(i, Tuple<Float,Float,Empty>(f, []));
The error I get on the first two revolves around the use of brackets:
Incorrect syntax: missing statement-ending ; at [ expecting statement-ending ;
There are two separate errors on the second:
Some variation of
Alias parameter distance must be assignable to corresponding class parameter rest: Integer is not assignable to [Integer]
on class MyPair and
Argument must be a parameter reference to distance
on f, [f], or the tuple construction.
Is there a way to do this?
Yeah, the instantiation expression on the RHS of the => in a class alias declaration is currently extremely restricted, not by design, but just because it will take some extra work to implement full support for arbitrary instantiation expressions in the compiler backends.
But what I would actually do for now would be to use a regular type alias, like this:
alias MyPair => [Integer,Float];
And use it like this:
MyPair pair = [1, 1.0];
I think that's actually even cleaner than using a class alias.
HTH.
After tinkering around a bit I came across
class MyPair(Integer i, [Float] f) =>
Tuple<Integer|Float, Integer, Tuple<Float, Float, Empty>>(i, f);
which works.
Can't do much better than your solution, but you can at least use a shortcut for the Rest type parameter:
class Pair([Integer i, [Float] f]) => Tuple<Integer|Float, Integer, [Float]>(i, f);
You're limited here because the parameter types of your class alias must match the parameter types of the class that you're aliasing. If I'm interpreting the spec correctly:
Note: currently the compiler imposes a restriction that the callable type of the aliased class must be assignable to the callable type of the class alias. This restriction will be removed in future.
then this might work in subsequent releases:
class Pair(Integer i, Float f) => Tuple<Integer|Float, Integer, [Float]>(i, [f]);
or maybe even:
class Pair(Integer i, Float f) => [i, f];
Then again, if your aim is to destructure a tuple, Ceylon 1.2 will let you do that directly:
value [i, f] = [2, 0.5];
In other languages, we can have a function which takes no arguments. Can we have 0 argument function in ocaml?
Functions in OCaml have exactly one argument (ignoring complications due to optional arguments). So, you can't have a function with no arguments.
As #alfa64 says, you could consider a simple value as a function with no arguments. But it will always have the same value (which, in fact, makes it similar to a pure function).
If you want to write a function that doesn't actually require any arguments (one that has side effects, presumably), it is traditional to use () as its argument:
# let p () = Printf.printf "hello, world\n";;
val p : unit -> unit = <fun>
# p ();;
hello, world
- : unit = ()
#
In OCaml functions always have an arguments. Therefore, we might wonder how to translate the following say_hello C function in OCaml:
void
say_hello()
{
printf("Hello, world!\n");
}
There is a special type unit in OCaml which has only one value, written as (). While it might look odd and useless, it adds regularity to the language: a function not needing a specific argument can just take an argument of type unit, a function not returning a useful value usually returns a value of type unit. Here is how to translate the above say_hello function to OCaml:
# let say_hello () = print_endline "Hello, world!";;
val say_hello : unit -> unit = <fun>
Incidentally, template based meta-programming would be much easier in C++ if there were no type void but a similar unit type instead. It is quite common to treat separately functions having no arguments in template specialisations.
Object methods, while being similar to functions, do not require an argument.
# class example =
object
method a =
print_endline "This demonstrates a call to method a of class example"
end;;
class example : object method a : unit end
# let x = new example;;
val x : example = <obj>
# x # a ;;
This demonstrates a call to method a of class example
- : unit = ()
Instead of
let foo n = 55
You just
let foo = 55
And then call foo wherever.
What are the differences between type and class in Clojure?
(type "") => java.lang.String
(class "") => java.lang.String
(type 1) => java.lang.Long
(class 1) => java.lang.Long
According to ClojureDocs
type
type clojure.core
(type x)
Returns the :type metadata of x, or its Class if none
class
class clojure.core
(class x)
Returns the Class of x
So, basically if there're metadata inside x, type should return its :type metadata, otherwise they're the same thing.
Why can't I coerce record types in OCaml? Base types like int works fine.
Below is an example where I construct a base module M which I include in module A. M.t is type abbriviated in A. As long as M.t is int, I can do A.t' :> M.t. When I change it to {i : int}, the compiler says it's not a subtype. I'm guessing there is a very specific reason for this?
module M = struct
type t = {i : int}
let make () = {i = 10}
end
module A : sig
include module type of M
type t' = private t
val make : unit -> t'
end = struct
include M
type t' = t
end
In the toplevel:
(A.make() :> M.t);;
Error: Type A.t' is not a subtype of M.t
That's because A.t' has no relation to M.t, because include does not "preserve" equality, it just literally duplicates the module structure (or signature) and inlines it in place (as fresh types and values). So type M.t doesn't have any relation to A.t and therefore to A.t' (and different record types do not have structural subtyping defined like say objects or modules).
Obvious fix is type t' = private M.t.
UPDATE
It appears the above is not fully correct, because type t' = private M.t in signature and include M type t' = t in implemention do typecheck, so include M preserves the equality (otherwise it couldn't match the signature type t' = private M.t), unlike copypasting the contents of M in the place of include M. But this "obviously" doesn't hold for include module type of which creates fresh types..