Elm recursive type definition - elm

Just taking a peek at elm code and came across the following type definition:
type Cmd msg = Cmd
I just can't seem to understand how this works. Anybody can explain?

That isn't a recursive type definition, it's defining a type Cmd as a union type with one label, Cmd, which contains no extra information. Usually definitions like this are intended to signal that the type represents values that have no individual meaning in elm.
In this case, the type is defined that way because all operations on Cmd are hidden in platform code, so there's no need for users to be able to examine or destructure Cmd values. Cmd needs to expose a type variable in order to preserve the type safety of Cmd values, because they encapsulate a promise to yield a message of a given type and that type can be be changed via Cmd.map .

Related

abap reference variables and dynamic types

I'm fluent in ABAP and have a grasp on OO and light reference variables, but can't seem to get a deeper handle on reference variables and dynamic types etc. I've done a bit of reading, but can't seem to get the deep understanding I feel I need.
Does anyone know of some great tutorials or websites that might give clear and concise? Thanks!
First of all just google this post title and You're golden.
Second:
I'm not sure if I understand You correctly, do You want to know about such constructions as:
DATA lo_ref_var TYPE REF TO zcl_my_class.
And by dynamic types do You mean in ABAP 7.4/7.5 (ex. DATA(lv_var) = 123)?
If yes, I'll try to give You the general idea:
Reference variable is just a variable that's "ready to become" an object.
If You'll take this for example:
DATA lo_ref_var TYPE REF TO zcl_my_class.
CREATE OBJECT lo_ref_var.
Then assuming the constructor doesn't need any variables You'll get an instance of zcl_my_class Class with all it's attributes and methods. Also if You have an abstract class zcl_abs_class as a super-class and zcl_sub_class1 and zcl_sub_class2 as it's non-abstract subclass' than:
DATA:
lo_abs TYPE REF TO zcl_abs_class,
lo_sub1 TYPE REF TO zcl_sub_class1,
lo_sub2 TYPE REF TO zcl_sub_class2.
CREATE OBJECT: lo_sub1, lo_sub2.
lo_abs ?= lo_sub1.
lo_abs ?= lo_sub2.
What You can do (as seen above) is cast a subclass object to the super-class reference variable since the subclass' inherits from zcl_abs_class.
For more, do some digging.
Dynamic types:
This is in fact very simple, all You need to remember is that a variable has to have a type when being created dynamically. So for example:
DATA(lv_text) = text-000.
DATA(lv_int) = 1.
Line with lv_text will not work (will not compile) since text-000 does not have a precise type.
The second line on the other hand will take the type I.
If one would like to decide which type to choose You can do this by writing:
DATA(lv_bukrs) = CONV bukrs( '1234' ).
You can even use the type that an already existing variable has by writing:
DATA(lv_bukrs2) = CONV #( lv_bukrs ).
since the "#" means "use the type of variable inside brackets".
Hope this will help You start :)

What is the meaning of `Cmd msg`?

I'm trying to use ports to pass an URL to Javascript in order to redirect the user to another page. I wrote a port module to contain all the ports needed for my project :
port module Utils exposing (..)
port changePage : String -> Cmd Event
Then, I imported it in my Elm file :
type Event = PageChange String
import Utils exposing (changePage)
But the compiler didn't like it :
It looks like the keyword `import` is being used as a variable.
8| import Utils exposing (changePage)
^
Rename it to something else.
So I moved the port definition to the main file :
type Event = PageChange String
port changePage : String -> Cmd Event
But the compiler still didn't agree :
Port `changePage` has an invalid type.
28| port changePage : String -> Cmd Event
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
You are saying it should be:
String -> Platform.Cmd.Cmd BmC.Index.Event
But you need to use the particular format described here:
<http://guide.elm-lang.org/interop/javascript.html#ports>
So I went to see that particular format, port check : String -> Cmd msg. I didn't understand where that msg came from, so I went to check the code, and I still don't understand what that line means.
Where does msg come from? What does type Cmd msg = Cmd mean? Thanks in advance.
import statements have to come before any function or type definitions. That's the reason you got a compile error on this code:
type Event = PageChange String
import Utils exposing (changePage)
On your questions of ports: Elm's ports are a feature that are on the edge of the architecture. They allow for interop with a different language, so there is, for all intents and purposes, a little magic going on behind the scenes.
Other languages have similar "magical" constructs for interop with other languages. Haskell has Foreign Function Interface (FFI) and C# has the extern keyword to call external libraries.
The definition of Cmd is one of those pieces that doesn't really make sense by looking at the code.
type Cmd msg = Cmd
That doesn't really tell us much, but that's ok. It's more of a placeholder that the compiler fills in on compile time. Other languages do that too. Haskell often uses a bottom call of let x = x in x to signify a function that the compiler will actually implement.
So, unless you're truly interested in the implementation of that crossover between Elm and Javascript, it's ok it leave it to the imagination and accept the magic.
As for being able to specify a concrete type in your port, that's just another choice of the compiler team. Even though a Cmd Event is the only thing that could go through the port, they chose to force a generic type as the Cmd's type parameter. The exact spelling of Cmd msg isn't required. msg could be anything lowercase:
port check : String -> Cmd a
Port declaration in Elm by design require a very specific type definition annotation.
Firs of all I would recommend looking into Reading Types and especially in the paragraph mentioning type variables
After that, make sure you are familiar with Generic Data Structures and if the official guide doesn't help, you can look into my answer to the similar question Understanding generic union types in Elm
Ports are slightly confusing so I have opened an issue The guide on JavaScript interop should explain port function type definitions
Outgoing port
port out : String -> Cmd msg
| |
| |
Concrete type Generic type
of outgoing with `msg` type variable
data
Outgoing port never sends messages,
but you need to specify the type so the
compiler can infer, that you can use
this command to send the outgoing values
in any module, that imports "out" function
Incoming port
port in : (List String -> msg) -> Sub msg
| |
| |
A function, that accepts Has to be generic,
outgoing data and returns because it is going to be
a message. This function a subscription with the
is called "tagger" same type as "tagger" returns
Has to be generic, i.e. use
`msg` type variable
Your first compiler error is because your import succeeds a definition, but the required format is to have all imports preceding all definitions.
As for what msg in String -> Cmd msg means, it is simply a type variable - all lowercase type names in type annotations in Elm are type variables, while those beginning with an uppercase letter are concrete types. So it's the same as String -> Cmd a or String -> Cmd foo.
In Elm, like in Haskell and some other functional languages, if a type variable is in a type annotation, it is implicitly universally quantified. So we read the above type as
changePage : ∀msg.String → Cmd msg
that is, 'for any type msg, changePage can take a String to a Cmd of msg'.
This means the expression changePage "myPage.html" can be used anywhere in your code where you want a command of some type. This seems to make sense, because it represents the command of 'please send "myPage.html" to anyone subscribed to the changePage port'.

Elm Language What do the Multiple Types in a row (without the arrow) in a signature mean?

In the Elm language, I'm having a hard time explaining my question...
In these snippets in elm:
I understand the signature in something like
update : Msg -> Model -> Model
where the parameters / output is separated by arrows, but how do I read / grok things like:
Sub Msg
Program Never Model Msg
In:
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
In a type signature, parameter types are separated by ->, with the last type being the return value.
If there are no -> symbols, then it means it is a value of that type. In your main example, the type of main is Program Never Model Msg. It has no arrows, so it takes no parameters.
Now, each parameter and the return value in the type annotation may have several things separated by spaces, as in your main example. The leftmost is the type, followed by type parameters separated by spaces.
Program Never Model Msg
| | | |
| ------|-----
type type parameters
A type parameter is similar to Generics in a language like C#. The equivalent syntax in C# would be:
void Program<Never, Model, Msg>()
C# doesn't directly correlate because it has a different way of constraining generic type parameters, but the general idea holds.
The Elm guide doesn't currently have a great deal of info, but here is the section talking about types.
Sub Msg, List Int, Program Never Model Msg
Sub, List and Program are type constructors. You can think about them as functions that take a type and return another type.
By themselves, Sub, List and Program are not complete type. They are like a puzzle missing a piece. When one adds the missing piece, the puzzle is complete.
I usually read them in my head using the word of as in a List of Ints, a Program of Never, Model and Msg.

Single-line method calls with untyped parameters

Can I define an ABAP method where the RETURNING parameter and any IMPORTING parameters have a generic type but that can still be called in a single line as a functional method?
In other words I'd like to replace this:
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_external_value
IMPORTING
output = lv_internal_value.
With:
lv_internal_value= zcl_conversion=>alpha_input( lv_external_value ).
Unfortunately the fact that Class Methods can't have an untyped returning parameter is preventing me from declaring the functional method's return value as type ANY or CLIKE. The accepted standard of creating generic method parameters seems to be to define them as TYPE REF TO DATA and dereference/assign them. But as far as I know that prevents me from calling the method in a single statement as I have to first assign the importing parameter and then dereference the returning parameter, resulting in the same or more lines of code than a simple FM call.
Is there a way around this?
Unfortunately, there is no other way to dereference data than to use the dereference operator, either in the form ->* for the full value segment, or in the form ->comp, if the data object is structured and has a component named comp (and, even worse, there are a lot of places in ABAP code where you would like to use a value from a derefenced data object but can't do it for internal reasons / syntax restrictions).
However, you could simply keep the data reference object retrieved by your method in a variable of the calling code and work with that variable (instead of using a field symbol or a variable for the derefenced value segment itself). Either generically, as a ref to data variable, or typed, using the CAST operator (new ABAP syntax).
Most things that can be done with a field-symbol, can also be done directly with a data reference as well.
Example: Working with a variable result of the expected return type:
data(result) = cast t000( cl=>m( ) ).
write result->mandt.
See here the full example:
report zz_new_syntax.
class cl definition.
public section.
class-methods m returning value(s) type ref to data.
endclass.
start-of-selection.
data(result) = cast t000( cl=>m( ) ).
write: / result->mandt. " Writes '123'.
class cl implementation.
method m.
s = new t000( mandt = '123' ).
endmethod.
endclass.
On ABAP NW Stack 7.4 you could just use parameters type STRING and then use the new CONV Operator to convert your actual input in string. Little ugly but should work.
lv_internal_value = CONV #(zcl_conversion=>alpha_input( CONV #(lv_external_value) )).

GetType on generic types

I'm trying register presenters with Windsor using the convention based method but trying to do this in VB.NET, but the problem is it does not want to compile this statement:
Dim type = GetType(AbstractPresenter(Of))
I am getting : Too few type arguments to AbstractPresenter(Of TView, TPresenter)
Which I don't understand because this is a valid statement according to question. Also showing valid in other C# to VB.NET converters when converting typeof(AbstractPresenter<>).
Any ideas?
There are two type arguments, and you need to specify this, just as you would do for multi-dimensional arrays:
Dim type = GetType(AbstractPresenter(Of ,))
Looks weird, but now the compiler knows that AbstractPresenter expects two type arguments.
By the way, C# has the same requirement. So the above would be written as:
var type = typeof(AbstractPresenter<,>);