If I have an object that contains a variable of a specific type. Can I somehow access that type?
The following obviously doesn’t work:
public type ObjectType = object {
first: Text;
};
// not valid syntax!
let myVar: ObjectType.first = "John";
I’d ultimately like to access the Interface of an actor. I’d like to define a function that returns the same thing that the function of the interface returns (and takes the same args).
Not an expert but I generally do something as following:
public type ObjectType = {
first: Text;
};
let myVar: ObjectType = {
first = "John"
};
// or
let first: Text = "John";
let myVar: ObjectType = {
first
};
Thank you, that definitely helps me generally. I meant though that I could use the type Text from the object without ever creating the actual object. So no variable would directly be of type ObjectType but I could directly define a variable of sort of the specific inner type, which in that case is Text.
Do you know what I mean?
If there was a function on the ObjectType then I’d like to create a function with this specific type but without ever using the ObjectType directly. I just want the function to be of the same type as the function on the object. Hope that makes any sense…
For inner types I create other types, again not the expert and not sure it answer your question ()?
public type InnerType = {
first: Text;
};
public type ObjectType = {
something: InnerType;
};
let myVar: InnerType = {
first = "John"
};
let myVar2: ObjectType {
something = myVar
};
You can also rename primitive types the same way
public type let Firstname = Text;
public type ObjectType = {
first: Firstname;
};
// then
let first: Firstname = "John";
let myVar: ObjectType {
first
};
I’m now confused if my question made any sense. In a way, I was wondering if I can create a sub-object that has to have let’s say 2 specific properties of the “parent” object. And if I can make that a type.
public type ParentObject = {
first: Text;
second: Int;
third: Nat;
fourth: Bool;
};
Given the object type above could I have a type that needs to have both “first:Text” and “second:Nat” but dependent on whether the parent still has these properties? So if I’d change the parent type not to have these properties anymore I’d get an error.
But as I said this might not make any sense as we’d probably just define the types the other way around. Meaning that the parent would consist of the child and have some additional properties.
I’m asking because it would be nice if I could define an actor that needs to implement a part of the interface of another actor. But I think it doesn’t make much sense, my bad.
I didn’t exactly understand what you’d want to have, but I feel like you’re interested in subtyping. Could you have a look at this and this and tell me if this is useful?
I managed to do what I wanted (slightly less elegant than I hoped). I’ll have to think through if my question made generally sense or if I just thought of it the wrong way.