Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Language] class implementation #237

Open
igotfr opened this issue Aug 7, 2024 · 0 comments
Open

[Language] class implementation #237

igotfr opened this issue Aug 7, 2024 · 0 comments

Comments

@igotfr
Copy link

igotfr commented Aug 7, 2024

Dada doesn't need to be a completely new language, it can just be a simplification of Rust with some syntax sugar, no need for an OOP like Swift, Kotlin or Java:

Examples

Dada

class Rectangle(
    width: u32,
    height: u32,
) {
    fn square(size: u32) -> Self {
        Self(
            width: size,
            height: size,
        )
    }

    fn area(shared self) -> u32 {
        self.width * self.height
    }

    fn can_hold(shared self, other: shared Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }

    fn width(shared self) -> bool {
        self.width > 0
    }
}

Rust

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn square(size: u32) -> Self {
        Self {
            width: size,
            height: size,
        }
    }

    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }

    fn width(&self) -> bool {
        self.width > 0
    }
}

Dada

class Point<T>(
    x: T,
    y: T,
) <T>$<T> {
    fn new(x: T, y: T) -> Self {
        Self(x, y)
    }
} <T: PartialOrd>$<T> {
    async fn cmp_display(shared self) {
        if self.x >= self.y {
            print("The largest member is x = {self.x}").await
        } else {
            print("The largest member is y = {self.y}").await
        }
    }
} $<f32> {
    fn distance_from_origin(shared self) -> f32 {
        (self.x.powi(2)   self.y.powi(2)).sqrt()
    }
}

Rust

struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
}

impl Point<f32> {
    fn distance_from_origin(&self) -> f32 {
        (self.x.powi(2)   self.y.powi(2)).sqrt()
    }
}

impl<T: Display   PartialOrd> Point<T> {
    fn cmp_display(&self) {
        if self.x >= self.y {
            println!("The largest member is x = {}", self.x);
        } else {
            println!("The largest member is y = {}", self.y);
        }
    }
}

Dada

trait Summary {
    fn summarize(shared self) -> String;
}

class NewsArticle(
    headline: String,
    location: String,
    author: String,
    content: String,
) : Summary {
    fn summarize(shared self) -> String {
        "{self.headline}, by {self.author} ({self.location})"
    }
}

class Tweet(
    username: String,
    content: String,
    reply: bool,
    retweet: bool,
) : Summary {
    fn summarize(shared self) -> String {
        "{self.username}: {self.content}"
    }
}

Rust

pub trait Summary {
    fn summarize(&self) -> String;
}

pub struct NewsArticle {
    pub headline: String,
    pub location: String,
    pub author: String,
    pub content: String,
}

impl Summary for NewsArticle {
    fn summarize(&self) -> String {
        format!("{}, by {} ({})", self.headline, self.author, self.location)
    }
}

pub struct Tweet {
    pub username: String,
    pub content: String,
    pub reply: bool,
    pub retweet: bool,
}

impl Summary for Tweet {
    fn summarize(&self) -> String {
        format!("{}: {}", self.username, self.content)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant