Authoring in Scala #152
Replies: 6 comments 2 replies
-
@granicz FYI, this can be useful for the F# work too |
Beta Was this translation helpful? Give feedback.
-
How and where semantic errors in these models surface is an important aspect to consider. Morphir models in Scala or F# can only be directly executed if you can fail at compile time on all model code that doesn't map to MIR (it would be nasty to let things fail at runtime - and conversely, for all code ABC that compiles, runMIR(toMIR(ABC)) has the same semantics as ABC). This is not impossible (say, in F#, you can use type providers to generate code on the fly to include in the compilation), but not terribly easy nor practical to implement. So the most likely scenario would be to use these Scala/F# models to generate MIR (as JSON) and pass it along the pipeline for further processing/execution (or else fail during MIR generation). This can be done either by making the Scala/F# code result in MIR (through an SDK of source helpers that construct MIR - possibly at the expense of a slightly awkward syntax) - this always compiles, or by applying some heavy metaprogramming to construct MIR from the actual source code (traversing the reified source AST, doing semantic checks, using the mappings you identified to resolve/map types and functions, etc.) - which would be roughly similar to attempting to do the same at compile time using type providers, etc. |
Beta Was this translation helpful? Give feedback.
-
The level of detail that you've gone into in terms of mapping Scala features to Morphir features is impressive, and I can't wait to see how this project progresses. |
Beta Was this translation helpful? Give feedback.
-
Concerning these:
I'd suggest adding modules that map to the Morphir.SDK API as we've done for others. We can use type aliasing to point the actual type to the corresponding native types (i.e. java.time.Month for Scala). |
Beta Was this translation helpful? Give feedback.
-
Concerning this:
Where possible we should emit case classes and not rely on structural values as structural values have a heavy reflection based cost. Scala 3 does open the door for having something more performant under the covers for structural types, but I'd view this as a stretch goal. |
Beta Was this translation helpful? Give feedback.
-
Making in possible to write Morphir models in Scala has been an active topic of conversation in our community but we haven't publicly discussed the specifics yet. The goal of this post is to kick-start that process.
I think one of the first things we should decide is whether we want the Scala code that represents the business logic to be used as the logic that will be executed at runtime or just as a model that is still generated into executable Scala code. Maybe the former sounds like the trivial thing to do but it does affect what the mapping would look like.
The next thing would be how each language feature maps between Scala and Morphir. I took a stab at that and came up with the below. I looked at it from the Morphir feature perspective since that's the smaller language so it's easier to list out those features:
foo.bar
_.foo
val
ordef
within function body)def
within function body)foo.copy(bar = 1)
(only on case classes){}
This is what I have so far. What do you think?
@DamianReeves, @deusaquilus
Beta Was this translation helpful? Give feedback.
All reactions